Skip to content
Framework-agnostic, micro-library for getting stack traces in all web browsers https://www.stacktracejs.com/
JavaScript
Find file
Failed to load latest commit information.
dist Rename stacktrace-with-polyfills to indicate type of polyfills includ…
spec Switch to Jasmine 2 for testing. Fixes ajax testing in IE9
.editorconfig Add .editorconfig -- http://editorconfig.org
.gitignore Add bower dependencies
.jshintignore Convert reports to JSON
.jshintrc Convert reports to JSON
.npmignore Ignore build and editor files in node package
.nvmrc Convert to gulp-based build with optional polyfill
.travis.yml Update Sauce credentials
CHANGELOG.md Bump version to 1.0.0, update CHANGELOG and package for distribution
CONTRIBUTING.md Convert to gulp-based build with optional polyfill
LICENSE Implement StackTrace#get and StackTrace#fromError for v1.0
MIGRATION_GUIDE.md Preparing for 1.0
README.md Update Migration Guide link
bower.json Remove no-dep stacktrace.js from dist/. Fixes #140
component.json Bump version to 1.0.0, update CHANGELOG and package for distribution
gulpfile.js Rename stacktrace-with-polyfills to indicate type of polyfills includ…
karma.conf.ci.js Switch to Jasmine 2 for testing. Fixes ajax testing in IE9
karma.conf.js Switch to Jasmine 2 for testing. Fixes ajax testing in IE9
package.json Switch to Jasmine 2 for testing. Fixes ajax testing in IE9
polyfills.js Switch to Jasmine 2 for testing. Fixes ajax testing in IE9
stacktrace.js Convert reports to JSON

README.md

stacktrace.js

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

Build Status Coverage Status Code Climate

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.

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

Usage

Get a stack trace from current location

var callback = function(stackframes) {
    var stringifiedStack = stackframes.map(function(sf) { 
        return sf.toString(); 
    }).join('\n'); 
    console.log(stringifiedStack); 
};

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

StackTrace.get().then(callback).catch(errback)
=> Promise(Array[StackFrame], Error)
=> callback([StackFrame('func1', [], 'file.js', 203, 9), StackFrame('func2', [], 'http://localhost:3000/file.min.js', 1, 3284)])

window.onerror integration

Automatically handle errors

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

StackTrace.generateArtificially().then(callback).catch(errback)
=> Promise(Array[StackFrame], Error)

Trace every time a given function is invoked

// 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

Get stacktrace.js

npm install stacktrace-js
bower install stacktrace-js
component install stacktracejs/stacktrace.js
http://cdnjs.com/libraries/stacktrace.js

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.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 the arguments.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) => Promise(String)

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

Example JSON POST data:

{
  stack: [
    {functionName: 'fn', fileName: 'file.js', lineNumber: 32, columnNumber: 1},
    {functionName: 'fn2', fileName: 'file.js', lineNumber: 543, columnNumber: 32},
    {functionName: 'fn3', fileName: 'file.js', lineNumber: 8, columnNumber: 1}
  ]
}
  • stackframes: Array(StackFrame) - Previously wrapped Function
  • url: String - URL to POST stack JSON to

Browser Support

  • Chrome 1+
  • Firefox 3+
  • Safari 5+
  • Opera 9+
  • IE 6+
  • iOS 7+
  • Android 4.0+

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

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:

Something went wrong with that request. Please try again.