Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
aggregate-error
Advanced tools
The aggregate-error package is used to create an Error instance that aggregates multiple errors into a single error object. This can be useful when you have several operations that may each throw errors, and you want to collect all of those errors into a single error object that can be thrown, returned, or logged.
Aggregating multiple errors
This feature allows you to collect multiple errors into a single error object. The code sample demonstrates how to create an instance of AggregateError with an array of Error objects and then throw it.
const AggregateError = require('aggregate-error');
const errors = [
new Error('First error'),
new Error('Second error')
];
const aggregatedError = new AggregateError(errors);
throw aggregatedError;
Iterating over aggregated errors
This feature allows you to iterate over the individual errors within an AggregateError instance. The code sample shows how to loop through each error using a for...of loop and log them to the console.
const AggregateError = require('aggregate-error');
const errors = [
new Error('First error'),
new Error('Second error')
];
const aggregatedError = new AggregateError(errors);
for (const error of aggregatedError) {
console.error(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 aggregate-error in that it helps manage multiple error objects, but it focuses more on providing additional context and information about single errors rather than aggregating multiple errors.
Multi-error is another package that allows you to combine multiple errors into a single error object. It is similar to aggregate-error, but it has a different API and additional features such as filtering and mapping over the contained errors.
Create an error from multiple errors
$ npm install --save aggregate-error
const AggregateError = require('aggregate-error');
const err = new AggregateError([new Error('foo'), 'bar']);
throw err;
/*
AggregateError:
Error: foo
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:33)
Error: bar
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3)
at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
*/
for (const el of err) {
console.log(el);
}
//=> [Error: foo]
//=> [Error: bar]
Returns an Error
that is also an iterator
for the individual errors.
Type: Iterable<Error|string>
MIT © Sindre Sorhus
FAQs
Create an error from multiple errors
We found that aggregate-error 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.