Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate javascript stacktrace? [closed]

Any suggestions on how to, in a cross-browser way, generate a stack trace in javascript?

Newer browsers, Chrome and Firefox, expose a console object that allows stack traces to be generated. This method does not provide a method for storing the stack trace to a variable.

https://github.com/eriwen/javascript-stacktrace Works quite nicely, but it makes separate ajax requests to load script files included as part of the trace. This seems to be a common method in trace libraries. I'm guessing that browsers do not expose enough information to generate a meaningful stack-trace(line numbers, function names, file names, arguments, etc).

like image 693
Kyle Avatar asked Nov 20 '12 19:11

Kyle


1 Answers

Create an Error object and check it for a stack member. Adapted from Code Overtones:

var e = new Error('dummy');
var stack = e.stack.replace(/^[^\(]+?[\n$]/gm, '') // remove lines without '('
  .replace(/^\s+at\s+/gm, '') // remove prefix text ' at '
  .split('\n');
console.log(stack);

Error.stack is documented in Mozilla's reference documentation.

like image 98
Vincent Scheib Avatar answered Oct 06 '22 23:10

Vincent Scheib