34

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)?

Community
  • 1
  • 1
jaronimoe
  • 595
  • 1
  • 6
  • 12
  • How are you finding out these errors? Console? According to MDN (https://developer.mozilla.org/en/DOM/window.onerror) All errors that are sent to window (and presumably the console) should be picked up.... is dojo firing these events before your event is wired up? – Stuart.Sklinar Dec 18 '11 at 22:50
  • 5
    Try moving your window.onerror code before you include dojo.js. – Gavin Schulz Dec 22 '11 at 17:31

3 Answers3

18

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

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • 1
    Agreed. You might be able to use browser-specific debugging APIs, like the Venkman hook Firebug uses to implement its "Break on Error" feature, but those are generally only accessible from privileged code (i.e. Firefox extensions). – Sam Hanes Jan 24 '12 at 04:59
  • There is one problem with the try-catch logic though. I had some dynamic script which was damaged in structure, so that the catch-block was malformed eg. by adding one additional `}`. That effectively rendered the catch-block useless and i did not get any error-message to tell me what the problem was. – Frederic Leitenberger Oct 23 '14 at 10:58
  • Also this does not work with dojo's asynchronous script loading, in which case ```On(require, 'error', function(){});``` can be used instead – buggedcom Feb 24 '15 at 12:12
0

you can write code like this:

var goErrHandler=window.onerror;
goErrHandler= function(msg, url, linenumber) {
console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
return true;
}

goErrHandler();

so in console you'll see some thing like this :

Error message: undefined
URL: undefined 
Line Number: undefined
0

The better solution is to use try/catch, e.g.

try{
    if(a=='a'){

    }
}catch(e){
    alert(e);
    //or send to server
    new Image().src='errorReport.php?e='+e;
}

Google Plus seems to use this.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
dexbol
  • 47
  • 3
  • 2
    There is one problem with the try-catch logic though. I had some dynamic script which was damaged in structure, so that the catch-block was malformed eg. by adding one additional `}`. That effectively rendered the catch-block useless and i did not get any error-message to tell me what the problem was. – Frederic Leitenberger Oct 23 '14 at 10:59