-
-
Save j2doll/dfb6eb0b06639c3b1652e13acd7e5ea6 to your computer and use it in GitHub Desktop.
Get current stack and line number in JavaScript
This file contains 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
// For V8: | |
// Define global variable `__stack__` and `__line` | |
// for current stack and line | |
(function(global){ | |
Object.defineProperty(global, '__stack__', { | |
get: function(){ | |
var orig = Error.prepareStackTrace; | |
Error.prepareStackTrace = function(_, stack){ return stack; }; | |
var err = new Error; | |
Error.captureStackTrace(err, arguments.callee); | |
var stack = err.stack; | |
Error.prepareStackTrace = orig; | |
return stack; | |
} | |
}); | |
Object.defineProperty(global, '__line__', { | |
get: function(){ | |
return __stack__[1].getLineNumber(); | |
} | |
}); | |
})(this); | |
// For non-V8: | |
// but can not continue current excution | |
(function(global){ | |
var old = global.onerror; | |
global.onerror = function(errMsg, url, line){ | |
var oldReturn; | |
if(old) oldReturn = old.apply(global, arguments); | |
if(errMsg.indexOf('Get current line number') === -1) return; | |
console.log(line); | |
// if old error handler has return value, return it | |
if(oldReturn !== void 0){ // evaluate `undefined` that is not in global | |
return oldReturn; | |
} | |
// You can return `true` or `false` to prevent unexpected default behavior, | |
// for more detail: http://stackoverflow.com/questions/8087240/if-i-override-window-onerror-in-javascript-should-i-return-true-or-false | |
}; | |
})(this); | |
// Get current line number by such: | |
// throw new Error('Get current line number'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment