Created
July 29, 2011 22:13
-
-
Save wolframkriesing/1114854 to your computer and use it in GitHub Desktop.
Mini embeddable JavaScript stacktrace function (for the most relevant browsers)
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
// Create an error (normally you don't do that, but a try-catch :)) | |
var e = new Error(); | |
// Add a lineNumber and fileName property to the error in case there is none. | |
if (e.stack){ // Chrome has the "stack" property, but it's a rough string, parse out the lineNo and fileName. | |
var match = e.stack.split("\n")[1].match(/.*(http(s)?:\/\/.*):(\d+):(\d+)/); | |
if (match.length==5){ e.lineNumber = match[3]; e.fileName = match[1]; } | |
} else if (e.line && e.sourceURL){ // Safari has those | |
e.lineNumber = e.line; e.fileName = e.sourceURL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment