-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce GraphQLAggregateError
- Loading branch information
Showing
7 changed files
with
213 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* A GraphQLAggregateError is a container for multiple errors. | ||
* | ||
* This helper can be used to report multiple distinct errors simultaneously. | ||
* Note that error handlers must be aware aggregated errors may be reported so as to | ||
* properly handle the contained errors. | ||
* | ||
* See also: | ||
* https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-aggregate-error-objects | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError | ||
* https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.aggregate-error.js | ||
* https://github.com/sindresorhus/aggregate-error | ||
* | ||
*/ | ||
export class GraphQLAggregateError<T = Error> extends Error { | ||
readonly errors!: ReadonlyArray<T>; | ||
|
||
constructor(errors: ReadonlyArray<T>, message?: string) { | ||
super(message); | ||
|
||
Object.defineProperties(this, { | ||
name: { value: 'GraphQLAggregateError' }, | ||
message: { | ||
value: message, | ||
writable: true, | ||
}, | ||
errors: { | ||
value: errors, | ||
}, | ||
}); | ||
} | ||
|
||
get [Symbol.toStringTag](): string { | ||
return 'GraphQLAggregateError'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { GraphQLAggregateError } from '../GraphQLAggregateError'; | ||
|
||
describe('GraphQLAggregateError', () => { | ||
it('is a class and is a subclass of Error', () => { | ||
const suberrors = [new Error('Suberror1'), new Error('Suberror2')]; | ||
expect(new GraphQLAggregateError(suberrors)).to.be.instanceof(Error); | ||
expect(new GraphQLAggregateError(suberrors)).to.be.instanceof( | ||
GraphQLAggregateError, | ||
); | ||
}); | ||
|
||
it('has a name, errors, and a message (if provided)', () => { | ||
const suberrors = [new Error('Suberror1'), new Error('Suberror2')]; | ||
const e = new GraphQLAggregateError(suberrors, 'msg'); | ||
|
||
expect(e).to.include({ | ||
name: 'GraphQLAggregateError', | ||
errors: suberrors, | ||
message: 'msg', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.