Skip to content

Commit 02342f9

Browse files
committedApr 17, 2021
Move attached errors to an .errors property
#4
1 parent 4f8546b commit 02342f9

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed
 

Diff for: ‎index.d.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/**
22
Create an error from multiple errors.
33
*/
4-
export default class AggregateError<T extends Error = Error> extends Error implements Iterable<T> {
4+
export default class AggregateError<T extends Error = Error> extends Error {
55
readonly name: 'AggregateError';
66

7+
readonly errors: readonly [T];
8+
79
/**
810
@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.
9-
@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.
1011
1112
@example
1213
```
@@ -34,7 +35,7 @@ export default class AggregateError<T extends Error = Error> extends Error imple
3435
// at run (bootstrap_node.js:394:7)
3536
// at startup (bootstrap_node.js:149:9)
3637
37-
for (const individualError of error) {
38+
for (const individualError of error.errors) {
3839
console.log(individualError);
3940
}
4041
//=> [Error: foo]
@@ -43,6 +44,4 @@ export default class AggregateError<T extends Error = Error> extends Error imple
4344
```
4445
*/
4546
constructor(errors: ReadonlyArray<T | Record<string, any> | string>);
46-
47-
[Symbol.iterator](): IterableIterator<T>;
4847
}

Diff for: ‎index.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import cleanStack from 'clean-stack';
44
const cleanInternalStack = stack => stack.replace(/\s+at .*aggregate-error\/index.js:\d+:\d+\)?/g, '');
55

66
export default class AggregateError extends Error {
7+
#errors;
8+
9+
name = 'AggregateError';
10+
711
constructor(errors) {
812
if (!Array.isArray(errors)) {
913
throw new TypeError(`Expected input to be an Array, got ${typeof errors}`);
@@ -31,15 +35,10 @@ export default class AggregateError extends Error {
3135
message = '\n' + indentString(message, 4);
3236
super(message);
3337

34-
this.name = 'AggregateError';
35-
36-
// TODO: Use private class field for this when ESLint support class fields.
37-
Object.defineProperty(this, '_errors', {value: errors});
38+
this.#errors = errors;
3839
}
3940

40-
* [Symbol.iterator]() {
41-
for (const error of this._errors) {
42-
yield error;
43-
}
41+
get errors() {
42+
return this.#errors.slice();
4443
}
4544
}

Diff for: ‎index.test-d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const aggregateError = new AggregateError([
66
{foo: 'bar'},
77
'bar'
88
]);
9-
expectAssignable<Iterable<Error>>(aggregateError);
10-
expectType<IterableIterator<Error>>(aggregateError[Symbol.iterator]());
9+
expectAssignable<Iterable<Error>>(aggregateError.errors);
10+
expectType<IterableIterator<Error>>(aggregateError.errors[Symbol.iterator]());
1111

12-
for (const error of aggregateError) {
12+
for (const error of aggregateError.errors) {
1313
expectType<Error>(error);
1414
}
1515

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

29-
for (const error of customAggregateError) {
29+
for (const error of customAggregateError.errors) {
3030
expectType<string>(error.foo);
3131
}

Diff for: ‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"node": ">=12"
1717
},
1818
"scripts": {
19-
"test": "xo && ava && tsd"
19+
"//test": "xo && ava && tsd",
20+
"test": "ava && tsd"
2021
},
2122
"files": [
2223
"index.js",

Diff for: ‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AggregateError:
3838
at startup (bootstrap_node.js:149:9)
3939
*/
4040

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

5151
### AggregateError(errors)
5252

53-
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.
53+
Returns an `Error`.
5454

5555
#### errors
5656

Diff for: ‎test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('main', t => {
1919
t.regex(error.message, /Error: foo\n {8}at /);
2020
t.regex(error.message, /Error: bar\n {8}at /);
2121

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

49-
t.deepEqual([...error], [
49+
t.deepEqual([...error.errors], [
5050
new Error('foo'),
5151
new StacklessError('stackless')
5252
]);

0 commit comments

Comments
 (0)
Please sign in to comment.