Skip to content

Instantly share code, notes, and snippets.

@sh0bh1t
Last active September 10, 2015 12:31
Show Gist options
  • Select an option

  • Save sh0bh1t/102c8db089602cf9b023 to your computer and use it in GitHub Desktop.

Select an option

Save sh0bh1t/102c8db089602cf9b023 to your computer and use it in GitHub Desktop.
Javascript error handling without window.onerror
(function () {
var origCall = Function.prototype.call, myCaller;
var arraySlice = Array.prototype.slice;
var errRegex = /at(.*?)\((.*?):([0-9]{1,}):([0-9]{1,})\)/i;
var errReg = {
ff: /(.*?)\@(.*?)\:[0-9]{1,}/i
};
// IE 11 useragent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; SLCC2; rv:11.0) like Gecko
if( !navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/FireFox/) ){
errRegex = errReg.ff;
}
// Override call only if call_log element is present
(myCaller = function ( self ) {
Function.prototype.call = origCall;
try{
var returnValue = this.apply(self,arraySlice.call(arguments, 1));
}catch(e){
var rows = e.stack.split("\n"), script = {};
for( var i in rows ){
if( errRegex.test(rows[i]) ){
var tmp = errRegex.exec(rows[i]);
script = {name: tmp[1].trim(),url : tmp[2],line: tmp[3],char: tmp[4]};
break;
}
}
console.log(e.stack,script);
}
Function.prototype.call = myCaller;
return returnValue;
});
Function.prototype.call = myCaller;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment