window.onerror not working in chrome
Asked Answered
O

4

52

I am trying to add an onerror event to my website.

window.onerror = function() {
    alert("an error");
}

But all I receive is:

notThere();
ReferenceError: notThere is not defined

What am I missing?

Browser: Chrome 26.0.1410.64 m

Steps to reproduce:

  • add the code to the console.
  • add notThere() to the console
Oversubscribe answered 24/4, 2013 at 12:48 Comment(0)
W
43

The window.onerror works in Chrome (see jsfiddle - http://jsfiddle.net/PWSDF/), but apparently not in the console - which makes some sense.

Woolridge answered 21/10, 2013 at 22:37 Comment(5)
what sense does it make?Outburst
I guess, it would hijack errors instead of displaying them in the console.Woolridge
I assume it's for security and isolation. So the site doesn't have access to what the user is tampering with on the developer console. Not mentioning it would clutter the possible exception capturing or cause undefined behavior catching errors that didn't happen organically on the site.Detrimental
So no error that logs using the console can be captured? I'm not getting anything with window.location.href = "not://anywhere"; ... it just logs to the console and I can't capture it. How many errors are impossible to get like this?Empirical
@Empirical It can capture any exception that did NOT originate from the console (in some sense, the REPL loop acts as its own exception handler). However, errors that don't throw an exception will not fire an event. Your example fails because there is no uncaught exception. Similarly, this code does not trigger an alert: try {window.location.href = "not://anywhere";} catch{alert("NO");} .Nada
F
65

window.onerror is not triggered when the console directly generates an error. It can be triggered via setTimeout though, e.g., setTimeout(function() { notThere(); }, 0);

Possible duplicate: Chrome: Will an error in code invoked from the dev console trigger window.onerror?

Flywheel answered 2/12, 2014 at 19:51 Comment(0)
W
43

The window.onerror works in Chrome (see jsfiddle - http://jsfiddle.net/PWSDF/), but apparently not in the console - which makes some sense.

Woolridge answered 21/10, 2013 at 22:37 Comment(5)
what sense does it make?Outburst
I guess, it would hijack errors instead of displaying them in the console.Woolridge
I assume it's for security and isolation. So the site doesn't have access to what the user is tampering with on the developer console. Not mentioning it would clutter the possible exception capturing or cause undefined behavior catching errors that didn't happen organically on the site.Detrimental
So no error that logs using the console can be captured? I'm not getting anything with window.location.href = "not://anywhere"; ... it just logs to the console and I can't capture it. How many errors are impossible to get like this?Empirical
@Empirical It can capture any exception that did NOT originate from the console (in some sense, the REPL loop acts as its own exception handler). However, errors that don't throw an exception will not fire an event. Your example fails because there is no uncaught exception. Similarly, this code does not trigger an alert: try {window.location.href = "not://anywhere";} catch{alert("NO");} .Nada
M
28

Some other reasons that you may not be able to handle errors in window.onerror (apart from the mentioned ones):

  • Another library sets window.onerror after you.
  • If you are using Angular, errors wont pass through window.onerror. You have to handle them using this factory:

        .factory('$exceptionHandler', function() {
            return function errorCatcherHandler(exception, cause) {
                console.error(exception);
                if (window.OnClientError) window.OnClientError(exception);
            };
        })
    

See:

https://docs.angularjs.org/api/ng/service/$exceptionHandler

http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html

Milliner answered 15/9, 2015 at 20:28 Comment(0)
N
1

For me the problem was that the error was thrown in an async function, and errors thrown in async functions are handled by window.onunhandledrejection, not window.onerror (source: Why does onerror not intercept exceptions from promises and async functions). So if you want your error handler to work for errors from async functions, you need to assign it to window.onunhandledrejection as well:

window.onerror = window.onunhandledrejection = function(){
    alert("an error");
}

async function f(){
    notThere();
}

f();    //Alerts "an error"
Nonjoinder answered 4/7 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.