Jun 13, 2016

Catching JavaScript Errors with window.onerror

First we need to override the handler as:

window.onerror = function (errorMsg, url, lineNumber) {
    console.log('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
}

We can add two more variables to get more information: column number and error object.

window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
    console.log('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber 
    + ' Column: ' + column + ' StackTrace: ' +  errorObj);
}

Note: This will work only in the latest browsers.

No comments: