Stacktrace.js

Generate, parse, and enhance JavaScript stack traces in all web browsers

README

stacktrace.js

Generate, parse and enhance JavaScript stack traces in all browsers
Build Status  Coverage Status  GitHub license CDNJS size with dependencies gzip size module format

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
to return an Array of StackFrames.

Upgrading? Check the 0.x -> 1.x Migration Guide


Usage

Get a stack trace from current location

  1. ``` js
  2. var callback = function(stackframes) {
  3.     var stringifiedStack = stackframes.map(function(sf) {
  4.         return sf.toString();
  5.     }).join('\n');
  6.     console.log(stringifiedStack);
  7. };

  8. var errback = function(err) { console.log(err.message); };

  9. StackTrace.get().then(callback).catch(errback);
  10. //===> Promise(Array[StackFrame], Error)
  11. //===> callback([
  12. //    StackFrame({functionName: 'func1', args: [], fileName: 'file.js', lineNumber: 203, columnNumber: 9}),
  13. //    StackFrame({functionName: 'func2', args: [], fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284})
  14. //])
  15. ```

You can also get a stack trace synchronously

HEADS UP: This method does not resolve source maps or guess anonymous function names.

  1. ``` js
  2. StackTrace.getSync();
  3. //==> [
  4. //      StackFrame({functionName: 'func1', args: [], fileName: 'file.js', lineNumber: 203, columnNumber: 9}),
  5. //      StackFrame({functionName: 'func2', args: [], fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284})
  6. //]
  7. ```

window.onerror integration

Automatically handle errors
  1. ``` js
  2. window.onerror = function(msg, file, line, col, error) {
  3.     // callback is called with an Array[StackFrame]
  4.     StackTrace.fromError(error).then(callback).catch(errback);
  5. };
  6. ```

Get stack trace from an Error

  1. ``` js
  2. var error = new Error('BOOM!');

  3. StackTrace.fromError(error).then(callback).catch(errback);
  4. //===> Promise(Array[StackFrame], Error)
  5. ```

Generate a stacktrace from walking arguments.callee

This might capture arguments information, but isn't supported in ES5 strict-mode
  1. ``` js
  2. StackTrace.generateArtificially().then(callback).catch(errback);
  3. //===> Promise(Array[StackFrame], Error)
  4. ```

Trace every time a given function is invoked

  1. ``` js
  2. // callback is called with an Array[StackFrame] every time wrapped function is called
  3. var myFunc = function(arg) { return 'Hello ' + arg; };
  4. var myWrappedFunc = StackTrace.instrument(myFunc, callback, errback);
  5. //===> Instrumented Function
  6. myWrappedFunc('world');
  7. //===> 'Hello world'

  8. // Use this if you overwrote you original function
  9. myFunc = StackTrace.deinstrument(myFunc);
  10. //===> De-instrumented Function
  11. ```

Get stacktrace.js

  1. ```
  2. npm install stacktrace-js
  3. bower install stacktrace-js
  4. component install stacktracejs/stacktrace.js
  5. http://cdnjs.com/libraries/stacktrace.js
  6. ```

API


`StackTrace.get(/optional/ options)` => Promise(Array[StackFrame])

Generate a backtrace from invocation point, then parse and enhance it.

(Optional) options: Object
filter: Function(StackFrame => Boolean) - Only include stack entries matching for which filter returns true
sourceCache: Object (String URL => String Source) - Pre-populate source cache to avoid network requests
offline: Boolean (default: false) - Set to true to prevent all network requests

`StackTrace.getSync(/optional/ options)` => Array[StackFrame]

Generate a backtrace from invocation point, then parse it. This method does not use source maps or guess anonymous functions.  

(Optional) options: Object
filter: Function(StackFrame => Boolean) - Only include stack entries matching for which filter returns true

`StackTrace.fromError(error, /optional/ options)` => Promise(Array[StackFrame])

Given an Error object, use error-stack-parser
to parse it and enhance location information with stacktrace-gps.

error: Error

(Optional) options: Object
filter: Function(StackFrame => Boolean) - Only include stack entries matching for which filter returns true
sourceCache: Object (String URL => String Source) - Pre-populate source cache to avoid network requests
offline: Boolean (default: false) - Set to true to prevent all network requests

`StackTrace.generateArtificially(/optional/ options)` => Promise(Array[StackFrame])

Use stack-generator to generate a backtrace by walking thearguments.callee.caller chain.

(Optional) options: Object
filter: Function(StackFrame => Boolean) - Only include stack entries matching for which filter returns true
sourceCache: Object (String URL => String Source) - Pre-populate source cache to avoid network requests
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.

fn: Function - Instrumented Function

StackTrace.report(stackframes, url, message, requestOptions) => Promise(String)

Given an an error message and Array of StackFrames, serialize and POST to given URL. Promise is resolved with response text from POST request.

Example JSON POST data:
  1. ```
  2. {
  3.   message: 'BOOM',
  4.   stack: [
  5.     {functionName: 'fn', fileName: 'file.js', lineNumber: 32, columnNumber: 1},
  6.     {functionName: 'fn2', fileName: 'file.js', lineNumber: 543, columnNumber: 32},
  7.     {functionName: 'fn3', fileName: 'file.js', lineNumber: 8, columnNumber: 1}
  8.   ]
  9. }
  10. ```

stackframes: Array(StackFrame) - Previously wrapped Function
url: String - URL to POST stack JSON to
message: String - The error message
requestOptions: Object - HTTP request options object. Only headers: {key: val} is supported.

Browser Support Sauce Test Status


HEADS UP: You won't get the benefit of source maps

in IE9- or other very old browsers.

Using node.js/io.js only?

I recommend the stack-trace node package specifically built for node.
It has a very similar API and also supports source maps.

Contributing

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.

Want to be listed as a Contributor? Start with the Contributing Guide!

This project is made possible due to the efforts of these fine people: