Skip to content

Instantly share code, notes, and snippets.

@wolframkriesing
Created July 29, 2011 22:13
Show Gist options
  • Save wolframkriesing/1114854 to your computer and use it in GitHub Desktop.
Save wolframkriesing/1114854 to your computer and use it in GitHub Desktop.
Mini embeddable JavaScript stacktrace function (for the most relevant browsers)
// 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