stacktrace.js

Framework-agnostic, micro-library for getting stack traces in all web browsers.

Simple Usage

Just include stacktrace.js file on your page, and call it like so:

<script type="text/javascript" src="path/to/stacktrace.js" />
<script type="text/javascript">
    ... your code ...
    if (errorCondition) {
         var trace = printStackTrace();
         //Output however you want!
         alert(trace.join('\n\n'));
    }
    ... more code of yours ...
</script>

Custom Error Stack Traces

You can also pass in your own Error to get a stacktrace not available in IE 9- or Safari 5-

var lastError;
try {
    // error producing code
} catch(e) {
   lastError = e;
   // do something else with error
}

// Returns stacktrace from lastError!
printStackTrace({e: lastError});

Function Instrumentation

You can have any (public or privileged) function give you a stacktrace when it is called:

var p = new printStackTrace.implementation();
p.instrumentFunction(this, 'baz', logStackTrace);
function logStackTrace(stack) {
    console.log(stack.join('\n'));
}
function foo() {
    var a = 1;
    bar();
}
function bar() {
    baz();
}
foo(); //Will log a stacktrace when 'baz()' is called containing 'foo()'!

p.deinstrumentFunction(this, 'baz'); //Remove function instrumentation

Browser Support

Tested working in: