Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improving our javascript error reporting

So right now we have some generic code to report errors either from our code or third party code. Our project is a JQM/Phonegap project for iOS. What is happening is we pretty much always get the same useless error... TypeError: 'undefined' is not a function... with no line number or other helpful information. Is there a way I could change the code to maybe get WHAT is undefined or WHERE it is?

window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
    //Handle errors not in a jquery event handler
    //DebugMessage(errorMSg + " "+ url + " " + lineNumber);
    var ex = new Error(errorMsg, url, lineNumber);
    HandleError(ex, "window.onerror");  
        //HandleError sends the error object to 
        //a webservice to log the error.
    return true;
};

Any tips on debugging javascript errors would help as well.

like image 216
Twomz Avatar asked Jan 19 '12 22:01

Twomz


1 Answers

In recent months, browsers have extended the signature of window.onerror to provide more information.

window.onerror = function(msg, file, line, col, error) {

  // backwards compat
  if (!error) {
    error = new Error(msg);
  }

  // send error to your logger

}

This should give you a lot more information. But there are still things where you need better context. You should check out some third-party tools for this like TrackJS that automatically give you this, plus extra information on how the error occurred.

Disclaimer: I am one of the original authors of TrackJS, so I know a bunch about JavaScript errors :)

like image 84
Todd Gardner Avatar answered Nov 14 '22 09:11

Todd Gardner