Last active
September 10, 2015 12:31
-
-
Save sh0bh1t/102c8db089602cf9b023 to your computer and use it in GitHub Desktop.
Javascript error handling
without window.onerror
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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