Skip to content

Commit

Permalink
Move attached errors to an .errors property
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 17, 2021
1 parent 4f8546b commit 02342f9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
9 changes: 4 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
Create an error from multiple errors.
*/
export default class AggregateError<T extends Error = Error> extends Error implements Iterable<T> {
export default class AggregateError<T extends Error = Error> extends Error {
readonly name: 'AggregateError';

readonly errors: readonly [T];

/**
@param errors - If a string, a new `Error` is created with the string as the error message. If a non-Error object, a new `Error` is created with all properties from the object copied over.
@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.
@example
```
Expand Down Expand Up @@ -34,7 +35,7 @@ export default class AggregateError<T extends Error = Error> extends Error imple
// at run (bootstrap_node.js:394:7)
// at startup (bootstrap_node.js:149:9)
for (const individualError of error) {
for (const individualError of error.errors) {
console.log(individualError);
}
//=> [Error: foo]
Expand All @@ -43,6 +44,4 @@ export default class AggregateError<T extends Error = Error> extends Error imple
```
*/
constructor(errors: ReadonlyArray<T | Record<string, any> | string>);

[Symbol.iterator](): IterableIterator<T>;
}
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import cleanStack from 'clean-stack';
const cleanInternalStack = stack => stack.replace(/\s+at .*aggregate-error\/index.js:\d+:\d+\)?/g, '');

export default class AggregateError extends Error {
#errors;

name = 'AggregateError';

constructor(errors) {
if (!Array.isArray(errors)) {
throw new TypeError(`Expected input to be an Array, got ${typeof errors}`);
Expand Down Expand Up @@ -31,15 +35,10 @@ export default class AggregateError extends Error {
message = '\n' + indentString(message, 4);
super(message);

this.name = 'AggregateError';

// TODO: Use private class field for this when ESLint support class fields.
Object.defineProperty(this, '_errors', {value: errors});
this.#errors = errors;
}

* [Symbol.iterator]() {
for (const error of this._errors) {
yield error;
}
get errors() {
return this.#errors.slice();
}
}
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const aggregateError = new AggregateError([
{foo: 'bar'},
'bar'
]);
expectAssignable<Iterable<Error>>(aggregateError);
expectType<IterableIterator<Error>>(aggregateError[Symbol.iterator]());
expectAssignable<Iterable<Error>>(aggregateError.errors);
expectType<IterableIterator<Error>>(aggregateError.errors[Symbol.iterator]());

for (const error of aggregateError) {
for (const error of aggregateError.errors) {
expectType<Error>(error);
}

Expand All @@ -26,6 +26,6 @@ const customAggregateError = new AggregateError<CustomError>([
new CustomError('foo')
]);

for (const error of customAggregateError) {
for (const error of customAggregateError.errors) {
expectType<string>(error.foo);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
"//test": "xo && ava && tsd",
"test": "ava && tsd"
},
"files": [
"index.js",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AggregateError:
at startup (bootstrap_node.js:149:9)
*/

for (const individualError of error) {
for (const individualError of error.errors) {
console.log(individualError);
}
//=> [Error: foo]
Expand All @@ -50,7 +50,7 @@ for (const individualError of error) {

### AggregateError(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.
Returns an `Error`.

#### errors

Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('main', t => {
t.regex(error.message, /Error: foo\n {8}at /);
t.regex(error.message, /Error: bar\n {8}at /);

t.deepEqual([...error], [
t.deepEqual([...error.errors], [
new Error('foo'),
new Error('bar'),
Object.assign(new Error('baz'), {code: 'EBAZ'}),
Expand All @@ -46,7 +46,7 @@ test('gracefully handle Error instances without a stack', t => {
t.regex(error.message, /Error: foo\n {8}at /);
t.regex(error.message, /StacklessError: stackless/);

t.deepEqual([...error], [
t.deepEqual([...error.errors], [
new Error('foo'),
new StacklessError('stackless')
]);
Expand Down

0 comments on commit 02342f9

Please sign in to comment.