
Security News
Node.js Moves Toward Stable TypeScript Support with Amaro 1.0
Amaro 1.0 lays the groundwork for stable TypeScript support in Node.js, bringing official .ts loading closer to reality.
nested-error-stacks
Advanced tools
An Error subclass that will chain nested Errors and dump nested stacktraces
Supply Chain Security
Vulnerability
Quality
Maintenance
License
The nested-error-stacks npm package is used to create Error objects in Node.js that can encapsulate and display a stack trace from another error. This is useful for debugging and error handling when you want to preserve the original error context while adding additional information or handling an error at a higher level in your application.
Creating nested errors
This feature allows developers to create a new error that includes the stack trace of a previous error. The code sample demonstrates how to catch an error and throw a new NestedError that includes the original error's stack trace.
const NestedError = require('nested-error-stacks');
function doSomethingRisky() {
try {
// Code that might throw an error
} catch (error) {
throw new NestedError('An error occurred in doSomethingRisky', error);
}
}
The verror package provides a way to create rich JavaScript errors. It allows you to chain errors, add context to them, and format multi-line error messages. It is similar to nested-error-stacks but offers more features for constructing complex error objects.
The error-cause package is a polyfill for the Error.cause property, which is part of the ECMAScript proposal. It allows you to specify the cause of an error object, similar to nested-error-stacks, but it follows a proposed standard for JavaScript errors.
With this module, you can wrap a caught exception with extra context for better debugging. For example, a network error's stack would normally look like this:
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
Using this module, you can wrap the Error with more context to get a stack that looks like this:
NestedError: Failed to communicate with localhost:8080
at Socket.<anonymous> (/Users/mattlavin/Projects/nested-stacks/demo.js:6:18)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:415:13)
Caused By: Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
Here is an example program that uses this module to add more context to errors:
var NestedError = require('nested-error-stacks');
var net = require('net');
var client = net.connect({port: 8080});
client.on('error', function (err) {
var newErr = new NestedError("Failed to communicate with localhost:8080", err);
console.log(newErr.stack);
});
It is recommended to use explicit names for Error classes. You can do it like this:
var util = require('util');
var NestedError = require('nested-error-stacks');
function MyError(message, nested) {
NestedError.call(this, message, nested);
}
util.inherits(MyError, NestedError);
MyError.prototype.name = 'MyError';
FAQs
An Error subclass that will chain nested Errors and dump nested stacktraces
The npm package nested-error-stacks receives a total of 4,155,327 weekly downloads. As such, nested-error-stacks popularity was classified as popular.
We found that nested-error-stacks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Amaro 1.0 lays the groundwork for stable TypeScript support in Node.js, bringing official .ts loading closer to reality.
Research
A deceptive PyPI package posing as an Instagram growth tool collects user credentials and sends them to third-party bot services.
Product
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.