Skip to content

Commit

Permalink
Support plain objects (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfbecker authored and sindresorhus committed Dec 26, 2018
1 parent 7ae39e2 commit 156215d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ class AggregateError extends Error {
throw new TypeError(`Expected input to be iterable, got ${typeof errors}`);
}

errors = Array.from(errors).map(err => err instanceof Error ? err : new Error(err));
errors = Array.from(errors).map(err => {
if (err instanceof Error) {
return err;
}
if (err && typeof err === 'object') {
// Handle plain error objects with message property and/or possibly other metadata
return Object.assign(new Error(err.message), err);
}
return new Error(err);
});

let message = errors.map(err => cleanInternalStack(cleanStack(err.stack))).join('\n');
message = '\n' + indentString(message, 4);
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install --save aggregate-error
```js
const AggregateError = require('aggregate-error');

const err = new AggregateError([new Error('foo'), 'bar']);
const err = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);

throw err;
/*
Expand All @@ -24,6 +24,8 @@ AggregateError:
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)
Error: baz
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)
Expand All @@ -41,18 +43,22 @@ for (const el of err) {
}
//=> [Error: foo]
//=> [Error: bar]
//=> [Error: baz]
```


## API

### AggregateError(errors)

Returns an `Error` that is also an [`iterator`](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Iterators_and_Generators) for the individual errors.
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.

#### errors

Type: `Iterable<Error|string>`
Type: `Iterable<Error|Object|string>`

If a string, a new `Error` is created with the string as the error message.<br>
If a non-Error object, a new `Error` is created with all properties from the object copied over.


## License
Expand Down
14 changes: 12 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import test from 'ava';
import AggregateError from './';

test(t => {
const err = new AggregateError([new Error('foo'), 'bar']);
const err = new AggregateError([
new Error('foo'),
'bar',
{message: 'baz', code: 'EBAZ'},
{code: 'EQUX'}
]);
console.log(err);
t.regex(err.message, /Error: foo\n {8}at /);
t.regex(err.message, /Error: bar\n {8}at /);
t.deepEqual(Array.from(err), [new Error('foo'), new Error('bar')]);
t.deepEqual(Array.from(err), [
new Error('foo'),
new Error('bar'),
Object.assign(new Error('baz'), {code: 'EBAZ'}),
Object.assign(new Error(), {code: 'EQUX'})
]);
});

0 comments on commit 156215d

Please sign in to comment.