In version 1.x, We've switched from a synchronous API to an asynchronous one using Promises because synchronous ajax calls are deprecated and frowned upon due to performance implications.
All methods now return stackframes. This Object representation is modeled closely after StackFrame representations in Gecko and V8. All you have to do to get stacktrace.js v0.x behavior is call .toString() on a stackframe.
Use Case: Give me a trace from wherever I am right now
var error = new Error('Boom');
printStackTrace({e: error});
==> Array[String]
v1.x:
var error = new Error('Boom');
StackTrace.fromError(error).then(callback).catch(errback);
==> Promise(Array[StackFrame], Error);
If this is all you need, you don't even need the full stacktrace.js library! Just use error-stack-parser!
ErrorStackParser.parse(new Error('boom'));
Use Case: Give me a trace anytime this function is called
Instrumenting now takes Function references instead of Strings.
v0.x:
function interestingFn() {...};
var p = new printStackTrace.implementation();
p.instrumentFunction(this, 'interestingFn', logStackTrace);
==> Function (instrumented)
p.deinstrumentFunction(this, 'interestingFn');
==> Function (original)
v1.x:
function interestingFn() {...};
StackTrace.instrument(interestingFn, callback, errback);
==> Function (instrumented)
StackTrace.deinstrument(interestingFn);
==> Function (original)
Game on!
.parseError()
Error: Error message
at baz (http://url.com/file.js:10:7)
at bar (http://url.com/file.js:7:17)
at foo (http://url.com/file.js:4:17)
at http://url.com/file.js:13:21
Parsed Error
.get()
function foo() {
console.log('foo');
bar();
}
function bar() {
baz();
}
function baz() {
function showTrace(stack) {
var event = new CustomEvent('st:try-show', {detail: stack});
document.body.dispatchEvent(event);
}
function showError(error) {
var event = new CustomEvent('st:try-error', {detail: error});
document.body.dispatchEvent(event);
}
StackTrace.get()
.then(showTrace)
.catch(showError);
}
foo();
StackTrace output
stacktrace.js
Framework-agnostic, micro-library for getting stack traces in all web browsers
Debug and profile your JavaScript with a stack trace of function calls leading to an error (or any condition you specify).
stacktrace.js uses browsers' Error.stack mechanism to generate stack traces, parses them, enhances them with source maps and uses Promises to return an Array of StackFrames.
window.onerror = function(msg, file, line, col, error) {
// callback is called with an Array[StackFrame]
StackTrace.fromError(error).then(callback).catch(errback);
};
Get stack trace from an Error
var error = new Error('BOOM!');
StackTrace.fromError(error).then(callback).catch(errback)
==> Promise(Array[StackFrame], Error)
Generate a stacktrace from walking arguments.callee
This might capture arguments information, but isn't supported in ES5 strict-mode
// callback is called with an Array[StackFrame] every time
// the wrapped interestingFn is called
StackTrace.instrument(interestingFn, callback, errback)
==> Instrumented Function
StackTrace.deinstrument(interestingFn)
==> De-instrumented Function
offline: Boolean (default: false) - Set to true to prevent all network requests
StackTrace.instrument(fn, callback, /*optional*/ errback) => Function
Given a function, wrap it such that invocations trigger a callback that is called with a stack trace.
fn: Function - to wrap, call callback on invocation and call-through
callback: Function - to call with stack trace (generated by StackTrace.get()) when fn is called
(Optional) errback: Function - to call with Error object if there was a problem getting a stack trace. Fails silently (though fn is still called) if a stack trace couldn't be generated.
StackTrace.deinstrument(fn) => Function
Given a function that has been instrumented, revert the function to it's original (non-instrumented) state.
This library accepts a code location (in the form of a StackFrame) and returns a new StackFrame with a more accurate location (using source maps) and guessed function names.
Usage
var stackframe = new StackFrame({fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284});
var callback = function myCallback(foundFunctionName) { console.log(foundFunctionName); };
// Such meta. Wow
var errback = function myErrback(error) { console.log(StackTrace.fromError(error)); };
var gps = new StackTraceGPS();
// Pinpoint actual function name and source-mapped location
gps.pinpoint(stackframe).then(callback, errback);
//===> Promise(StackFrame({functionName: 'fun', fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
// Better location/name information from source maps
gps.getMappedLocation(stackframe).then(callback, errback);
//===> Promise(StackFrame({fileName: 'file.js', lineNumber: 203, columnNumber: 9}), Error)
// Get function name from location information
gps.findFunctionName(stackframe).then(callback, errback);
//===> Promise(StackFrame({functionName: 'fun', fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284}), Error)
Simple, cross-browser Error parser. This library parses and extracts function names, URLs, line numbers, and column numbers from the given Error's stack as an Array of StackFrames.
Once you have parsed out StackFrames, you can do much more interesting things. See stacktrace-gps.
Note that in IE9 and earlier, Error objects don't have enough information to extract much of anything. In IE 10, Errors are given a stack once they're thrown.