stacktracejs/stacktrace.js

Name: stacktrace.js

Owner: stacktrace.js

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

Created: 2010-01-20 16:20:47.0

Updated: 2018-02-21 19:35:16.0

Pushed: 2017-08-16 23:27:14.0

Homepage: https://www.stacktracejs.com/

Size: 9886

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

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

Upgrading? Check the 0.x -> 1.x Migration Guide
Usage
Get a stack trace from current location
callback = function(stackframes) {
var stringifiedStack = stackframes.map(function(sf) {
    return sf.toString();
}).join('\n');
console.log(stringifiedStack);


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

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

You can also get a stack trace synchronously

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

kTrace.getSync();
> [
    StackFrame({functionName: 'func1', args: [], fileName: 'file.js', lineNumber: 203, columnNumber: 9}), 
    StackFrame({functionName: 'func2', args: [], fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284})

window.onerror integration

Automatically handle errors

ow.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
error = new Error('BOOM!');

kTrace.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

kTrace.generateArtificially().then(callback).catch(errback);
=> Promise(Array[StackFrame], Error)
Trace every time a given function is invoked
allback is called with an Array[StackFrame] every time wrapped function is called
myFunc = function(arg) { return 'Hello ' + arg; };
myWrappedFunc = StackTrace.instrument(myFunc, callback, errback);
=> Instrumented Function
appedFunc('world');
=> 'Hello world'

se this if you overwrote you original function
nc = StackTrace.deinstrument(myFunc);
=> De-instrumented Function
Get stacktrace.js
install stacktrace-js
r install stacktrace-js
onent install stacktracejs/stacktrace.js
://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

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

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

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

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

(Optional) options: Object

StackTrace.instrument(fn, callback, /*optional*/ errback) => Function StackTrace.deinstrument(fn) => Function

Given a function that has been instrumented, revert the function to it's original (non-instrumented) state.

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:


ssage: 'BOOM',
ack: [
{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}


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:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.