Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

stacktracejs/stacktrace.js

Repository files navigation

Generate, parse and enhance JavaScript stack traces in all browsers

Build StatusCoverage StatusGitHub licenseCDNJSsize with dependenciesgzip sizemodule format

Debug and profile your JavaScript with astack 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 withsource maps and usesPromisesto return an Array ofStackFrames.

Upgrading? Check the0.x -> 1.x Migration Guide

Usage

Get a stack trace from current location

varcallback=function(stackframes){varstringifiedStack=stackframes.map(function(sf){returnsf.toString();}).join('\n');console.log(stringifiedStack);};varerrback=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

varerror=newError('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 calledvarmyFunc=function(arg){return'Hello '+arg;};varmyWrappedFunc=StackTrace.instrument(myFunc,callback,errback);//===> Instrumented FunctionmyWrappedFunc('world');//===> 'Hello world'// Use this if you overwrote you original functionmyFunc=StackTrace.deinstrument(myFunc);//===> De-instrumented Function

Get stacktrace.js

npm install stacktrace-jsbower install stacktrace-jscomponent install stacktracejs/stacktrace.jshttp://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 whichfilter returnstrue
  • sourceCache: Object (String URL => String Source) - Pre-populate source cache to avoid network requests
  • offline: Boolean (default: false) - Set totrue 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 whichfilter returnstrue

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

Given an Error object, useerror-stack-parserto parse it and enhance location information withstacktrace-gps.

error: Error

(Optional) options: Object

  • filter: Function(StackFrame => Boolean) - Only include stack entries matching for whichfilter returnstrue
  • sourceCache: Object (String URL => String Source) - Pre-populate source cache to avoid network requests
  • offline: Boolean (default: false) - Set totrue to prevent all network requests

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

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

(Optional) options: Object

  • filter: Function(StackFrame => Boolean) - Only include stack entries matching for whichfilter returnstrue
  • sourceCache: Object (String URL => String Source) - Pre-populate source cache to avoid network requests
  • offline: Boolean (default: false) - Set totrue 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 byStackTrace.get()) when fn is called

  • (Optional) errback: Function - to call with Error object if there was a problem getting a stack trace.Fails silently (thoughfn 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. Onlyheaders: {key: val} is supported.

Browser Support

Sauce Test Status

HEADS UP: You won't get the benefit ofsource mapsin IE9- or other very old browsers.

Using node.js/io.js only?

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

Contributing

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

Want to be listed as aContributor? Start with theContributing Guide!

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp