All Projects → stacktracejs → Stacktrace.js

stacktracejs / Stacktrace.js

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Stacktrace.js

validator
💯Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
Stars: ✭ 9,721 (+156.29%)
Mutual labels:  error-handling
Fluentresults
A generalised Result object implementation for .NET/C#
Stars: ✭ 266 (-92.99%)
Mutual labels:  error-handling
Data Structures
Go datastructures.
Stars: ✭ 336 (-91.14%)
Mutual labels:  error-handling
go-errors
Flexible, general-purpose error handling for Go.
Stars: ✭ 17 (-99.55%)
Mutual labels:  error-handling
chain
Extensible, Type Safe Error Handling in Haskell
Stars: ✭ 13 (-99.66%)
Mutual labels:  error-handling
Error Stack Parser
Extract meaning from JS Errors
Stars: ✭ 280 (-92.62%)
Mutual labels:  error-handling
errors
Simple error handling primitives that work well with structured logging
Stars: ✭ 28 (-99.26%)
Mutual labels:  error-handling
Stateviews
Create & Show progress, data or error views, the easy way!
Stars: ✭ 367 (-90.32%)
Mutual labels:  error-handling
Verifier
Package verifier provides simple defensive programing primitives.
Stars: ✭ 264 (-93.04%)
Mutual labels:  error-handling
Tslog
📝 tslog - Expressive TypeScript Logger for Node.js.
Stars: ✭ 321 (-91.54%)
Mutual labels:  error-handling
result
A lightweight C++11-compatible error-handling mechanism
Stars: ✭ 121 (-96.81%)
Mutual labels:  error-handling
bugsnag-wordpress
Bugsnag error monitoring for WordPress sites
Stars: ✭ 20 (-99.47%)
Mutual labels:  error-handling
Hamsters
A mini Scala utility library
Stars: ✭ 292 (-92.3%)
Mutual labels:  error-handling
domain-browser
Node's domain module for the web browser
Stars: ✭ 30 (-99.21%)
Mutual labels:  error-handling
Swift Validated
🛂 A result type that accumulates multiple errors.
Stars: ✭ 350 (-90.77%)
Mutual labels:  error-handling
rust-error-handle
detail rust error handle
Stars: ✭ 47 (-98.76%)
Mutual labels:  error-handling
Sentry Miniapp
Sentry 小程序/小游戏 SDK:用于小程序/小游戏平台的 Sentry SDK(目前支持微信、字节跳动、支付宝、钉钉、QQ、百度小程序,微信、QQ 小游戏)
Stars: ✭ 269 (-92.91%)
Mutual labels:  error-handling
Bugsnag React Native
Error monitoring and reporting tool for native exceptions and JS errors in React Native apps
Stars: ✭ 374 (-90.14%)
Mutual labels:  error-handling
Tbvaccine
A small utility to pretty-print Python tracebacks. ⛺
Stars: ✭ 358 (-90.56%)
Mutual labels:  error-handling
Rollbar Php
Error tracking and logging from PHP to Rollbar
Stars: ✭ 297 (-92.17%)
Mutual labels:  error-handling

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

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({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.

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

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 wrapped function is called
var myFunc = function(arg) { return 'Hello ' + arg; };
var myWrappedFunc = StackTrace.instrument(myFunc, callback, errback);
//===> Instrumented Function
myWrappedFunc('world');
//===> 'Hello world'

// Use this if you overwrote you original function
myFunc = StackTrace.deinstrument(myFunc);
//===> 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.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 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, 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:

{
  message: 'BOOM',
  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
  • 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:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].