What is capture-stack-trace?
The 'capture-stack-trace' npm package is a utility for capturing stack traces in Node.js. It is particularly useful for debugging and error handling, allowing developers to capture and manipulate stack traces programmatically.
What are capture-stack-trace's main functionalities?
Basic Stack Trace Capture
This feature allows you to capture the current stack trace and assign it to an object. The captured stack trace can then be logged or manipulated as needed.
const captureStackTrace = require('capture-stack-trace');
function myFunction() {
const obj = {};
captureStackTrace(obj);
console.log(obj.stack);
}
myFunction();
Custom Error Stack Trace
This feature allows you to capture a stack trace for a custom error class. By passing the custom error class as the second argument, you can ensure that the stack trace starts from the point where the error was instantiated.
const captureStackTrace = require('capture-stack-trace');
class CustomError extends Error {
constructor(message) {
super(message);
captureStackTrace(this, CustomError);
}
}
try {
throw new CustomError('Something went wrong!');
} catch (error) {
console.log(error.stack);
}
Other packages similar to capture-stack-trace
stack-trace
The 'stack-trace' package provides a more comprehensive API for working with stack traces in Node.js. It allows you to parse and manipulate stack traces, and offers more detailed information about each stack frame compared to 'capture-stack-trace'.
error-stack-parser
The 'error-stack-parser' package is designed to parse and extract information from error stack traces. It is particularly useful for front-end applications where you need to handle and display error information in a user-friendly manner. Unlike 'capture-stack-trace', it focuses on parsing existing stack traces rather than capturing them.
traceback
The 'traceback' package provides utilities for capturing and formatting stack traces in a more readable format. It offers additional features like filtering and formatting stack traces, making it a more feature-rich alternative to 'capture-stack-trace'.
capture-stack-trace
Ponyfill for Error.captureStackTrace
Install
$ npm install --save capture-stack-trace
Usage
var captureStackTrace = require('capture-stack-trace');
captureStackTrace({});
API
captureStackTrace(error)
error
Required
Type: Object
Target Object, that will recieve stack property.