Can `window.onerror` be used to log the error's stack trace as a global error handler

1.1k views Asked by At

I'm trying to make a global error handler to any javascript error happens in the browser, so I used this code in page's <head> tag:

window.onerror = function(msg, url, line, col, error) {
    // Note that col & error are new to the HTML 5 spec and may not be
    // supported in every browser.  It worked for me in Chrome.
    var extra = !col ? '' : '\ncolumn: ' + col;
    extra += !error ? '' : '\nerror: ' + error;

    // You can view the information in an alert to see things working like this:
    alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);

    // TODO: Report this error via ajax so you can keep track
    //       of what pages have JS issues

    var suppressErrorAlert = true;
    // If you return true, then error alerts (like in older versions of
    // Internet Explorer) will be suppressed.
    return suppressErrorAlert;
};

But since I'm using a CDN with minifying my javascript files, I always get these useless values passed to the function:

msg: Script error.

url: ''

line: 0

col: 0

error: null

(results from Chrome Version 43.0.2357.125 (64-bit) on Ubuntu)

So, is there anyway I can get the error's stack trace (or even any info about it) from inside the window.onerror?

0

There are 0 answers