3

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?

Community
  • 1
  • 1
AbdelHady
  • 9,334
  • 8
  • 56
  • 83
  • Does this work without a CDN (minification)? If yes then perhaps it's the minification process that mingled with your variable names. – Omar Ali Jun 16 '15 at 12:26
  • I've tried to disable the minification for a try, but nothing changed, I always get the same results. – AbdelHady Jun 16 '15 at 12:28
  • see http://stackoverflow.com/a/635852/6782 – Alnitak Jun 16 '15 at 12:48
  • but most of the time it gives me a `null` error, so I can't use it to get the stack as per the answer you mentioned – AbdelHady Jun 16 '15 at 13:20
  • Any solution to this @AbdelHady ? – Peege151 Jun 20 '16 at 20:55
  • @Peege151, until now the best possible solution is to use services like https://trackjs.com/ or https://errorception.com/ , but unfortunatley, doing it manually like the question here doesn't guarantee getting the actual error data – AbdelHady Jun 21 '16 at 11:23

0 Answers0