Search code examples
javascriptsentryraven

_oldOnerrorHandler is undefined; if statement still runs


I have a page with raven on it. The URL can be seen in screenshots, but is irrelevant.

In Sentry (a service at which raven errors are logged), I see a few thousand errors related to calling an undefined function, which brought me to this breakpoint.

enter image description here

From the screenshot, the variable _oldOnerrorHandler is undefined. Both the watched expression of _oldOnerrorHandler and the _oldOnerrorHandler variable are undefined at this point. Therefore, the line if (_oldOnerrorHandler) should never execute.

enter image description here

The screenshot above is evident that the contents of the if statement is indeed being run, which means _oldOnerrorHandler is not falsy. How is this possible in JavaScript, and what possible solutions exist to prevent this error?


Solution

  • The answer is actually quite simple: _oldOnerrorHandler actually is defined, in the context of the function you are debugging - that's why the line in question executes. However, it isn't defined in the context of the window which is the context used by the console.

    If you look at the Raven.js source code, the variable _oldOnerrorHandler isn't defined globally but rather inside a function. The console in Chrome doesn't run code inside the function you are debugging but rather globally - which means that as far as this code is concerned there is indeed no variable called _oldOnerrorHandler. Note that you get ReferenceError which means an undeclared variable, a declared variable with undefined as value would actually display just fine in the console.

    I cannot immediately see why your watch expression displays <not available>. But note that <not available> doesn't mean undefined - it rather means that Chrome is unable to calculate the value for this expression.

    If everything else fails, you can still instrument the Raven.js code to help you debug - add console.log(typeof _oldOnerrorHandler); line, my guess is that it will print function to console.