I want to log JavaScript errors so I'm overriding window.onerror
like this:
window.onerror = function(message, file, lineNumber) { var browser_ = encodeURI(navigator.appVersion); var error_ = encodeURI("msg:"+ message + "\n\tfile:"+file+"\n\tln:"+lineNumber); var user_ = encodeURI(""); ... return false; }
I've seen some people return true
and some return false
. Which is right and why? One post mentioned something about have you have to return true or Firefox will handle the error it's own way. What??
Definition and Usage The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image). Tip: When used on audio/video media, related events that occurs when there is some kind of disturbance to the media loading process, are: onabort.
The onerror() method These are: Error message – It is the same message that the browser would display for the given error. URL – It is the file location where the error occurred. Line number – It is the line number in the given URL that caused the error.
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.
From MDN on window.onerror
:
When the function returns
true
, this prevents the firing of the default event handler.
See also chromium Issue 92062:
In Chrome, returning
true
from window.onerror allows the error to propagate, and returningfalse
suppresses it.This is the inverse of the behavior in Firefox and IE, where returning 'true' suppresses the error, but returning
false
propagates it.
Note: the issue mentioned above was fixed, behavior is now as mentioned on MDN for all browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With