1 | # aggregate-error [![Build Status](https://travis-ci.org/sindresorhus/aggregate-error.svg?branch=master)](https://travis-ci.org/sindresorhus/aggregate-error)
|
---|
2 |
|
---|
3 | > Create an error from multiple errors
|
---|
4 |
|
---|
5 |
|
---|
6 | ## Install
|
---|
7 |
|
---|
8 | ```
|
---|
9 | $ npm install aggregate-error
|
---|
10 | ```
|
---|
11 |
|
---|
12 |
|
---|
13 | ## Usage
|
---|
14 |
|
---|
15 | ```js
|
---|
16 | const AggregateError = require('aggregate-error');
|
---|
17 |
|
---|
18 | const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);
|
---|
19 |
|
---|
20 | throw error;
|
---|
21 | /*
|
---|
22 | AggregateError:
|
---|
23 | Error: foo
|
---|
24 | at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:33)
|
---|
25 | Error: bar
|
---|
26 | at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
|
---|
27 | Error: baz
|
---|
28 | at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
|
---|
29 | at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3)
|
---|
30 | at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
|
---|
31 | at Module._compile (module.js:556:32)
|
---|
32 | at Object.Module._extensions..js (module.js:565:10)
|
---|
33 | at Module.load (module.js:473:32)
|
---|
34 | at tryModuleLoad (module.js:432:12)
|
---|
35 | at Function.Module._load (module.js:424:3)
|
---|
36 | at Module.runMain (module.js:590:10)
|
---|
37 | at run (bootstrap_node.js:394:7)
|
---|
38 | at startup (bootstrap_node.js:149:9)
|
---|
39 | */
|
---|
40 |
|
---|
41 | for (const individualError of error) {
|
---|
42 | console.log(individualError);
|
---|
43 | }
|
---|
44 | //=> [Error: foo]
|
---|
45 | //=> [Error: bar]
|
---|
46 | //=> [Error: baz]
|
---|
47 | ```
|
---|
48 |
|
---|
49 |
|
---|
50 | ## API
|
---|
51 |
|
---|
52 | ### AggregateError(errors)
|
---|
53 |
|
---|
54 | Returns an `Error` that is also an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables) for the individual errors.
|
---|
55 |
|
---|
56 | #### errors
|
---|
57 |
|
---|
58 | Type: `Array<Error|Object|string>`
|
---|
59 |
|
---|
60 | If a string, a new `Error` is created with the string as the error message.<br>
|
---|
61 | If a non-Error object, a new `Error` is created with all properties from the object copied over.
|
---|