Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch ALL javascript errors with window.onerror? (including dojo)

this question is a follow-up to javascript: how to display script errors in a popup alert? where it was explained how to catch regular javascript errors using:

<script type="text/javascript">     window.onerror = function(msg, url, linenumber) {         alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);         return true;     } </script> 

I tried it and found out that dojo erros like this one:

TypeError: this.canvas is undefined         dojo.js (Row 446) 

were not reported using this method, which leads me to my question:

How can I report all javascript errors using window.onerror (especially dojo errors)?

like image 814
jaronimoe Avatar asked Dec 09 '11 14:12

jaronimoe


People also ask

What does Onerror do in JavaScript?

onerror is a special browser event that fires whenever an uncaught JavaScript error has been thrown. It's one of the easiest ways to log client-side errors and report them to your servers. It's also one of the major mechanisms by which Sentry's client JavaScript integration (raven-js) works.

Which JavaScript function is used for finding errors?

JavaScript try and catch The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.


1 Answers

It could be Dojo is using proper Error handling methods (i.e. try-catch blocks) which prevents the exception from bubbling up and reaching the window container, on which you have registered the error handler.

If so, there is no way for you to do this. No error is going past the catch block, so no error handler is being called.

As pointed out by the comments, you can also use browser-specific debugging APIs like the Venkman hook and do break-on-error -- a solution that usually only works for privileged code (thanks to @Sam Hanes).

You can also do On(require, 'error', function () {}); to add error handling on DOJO's asynchronous script loader -- another point mentioned in the comments by @buggedcom

like image 140
Milad Naseri Avatar answered Sep 20 '22 14:09

Milad Naseri