What is stacktrace-js?
The stacktrace-js package is a JavaScript library that provides stack traces for debugging JavaScript applications. It helps developers to better understand and debug their code by providing detailed information about the call stack when an error occurs.
What are stacktrace-js's main functionalities?
Generating stack traces
This feature allows developers to generate and capture stack traces within their applications. The code sample demonstrates how to obtain a stack trace and convert it into a readable string format.
StackTrace.get().then(function(stackframes) { var stringifiedStack = stackframes.map(function(sf) { return sf.toString(); }).join('\n'); console.log(stringifiedStack); });
Instrumenting functions to automatically attach stack traces
This feature automatically attaches stack traces to errors thrown in instrumented functions. The code sample shows how to instrument a function and handle errors with attached stack traces.
function foo() { throw new Error('Bazinga!'); } foo = StackTrace.instrument(foo, function(stackframes) { var stringifiedStack = stackframes.map(function(sf) { return sf.toString(); }).join('\n'); console.log(stringifiedStack); }, function(err) { console.log(err.message); }); foo();
Other packages similar to stacktrace-js
error-stack-parser
This package parses JavaScript Error stacks. It is similar to stacktrace-js but focuses more narrowly on parsing existing error stacks rather than generating new ones or instrumenting functions.
stack-generator
Stack Generator provides a way to generate artificial backtraces to help simulate stack traces. While stacktrace-js captures real stack traces, stack-generator is useful for creating synthetic stack traces for testing or debugging purposes.
Welcome to stacktrace.js!
A JavaScript tool that allows you to debug your JavaScript by giving you a stack trace of function calls leading to an error (or any condition you specify)
Usage
Just include stacktrace.js file on your page, and call it like so:
<script type="text/javascript" src="https://rawgithub.com/stacktracejs/stacktrace.js/master/stacktrace.js"></script>
<script type="text/javascript">
var trace = printStackTrace();
alert(trace.join('\n\n'));
</script>
You can also pass in your own Error to get a stacktrace not available in IE or Safari 5-
<script type="text/javascript" src="https://rawgithub.com/stacktracejs/stacktrace.js/master/stacktrace.js"></script>
<script type="text/javascript">
try {
} catch(e) {
var trace = printStackTrace({e: e});
alert('Error!\n' + 'Message: ' + e.message + '\nStack trace:\n' + trace.join('\n'));
}
</script>
Note that error message is not included in stack trace.
Bookmarklet available on the project home page.
Function Instrumentation
You can now have any (public or privileged) function give you a stacktrace when it is called:
function logStackTrace(stack) {
console.log(stack.join('\n'));
}
var p = new printStackTrace.implementation();
p.instrumentFunction(this, 'baz', logStackTrace);
function foo() {
var a = 1;
bar();
}
function bar() {
baz();
}
foo();
p.deinstrumentFunction(this, 'baz');
Get stacktrace.js
npm install stacktrace-js
bower install stacktrace-js
component install stacktrace.js
wget https://rawgithub.com/stacktracejs/stacktrace.js/master/stacktrace.js
Browser Support
It is currently tested and working on:
- Firefox (and Iceweasel) 0.9+
- Google Chrome 1+
- Safari 3.0+ (including iOS 1+)
- Opera 7+
- IE 5.5+
- Konqueror 3.5+
- Flock 1.0+
- SeaMonkey 1.0+
- K-Meleon 1.5.3+
- Epiphany 2.28.0+
- Iceape 1.1+
- PhantomJS
Contributions
This project is made possible due to the efforts of these fine people: