Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass and Check for Error Extensions #925

Merged
merged 5 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
[@jure](https://github.com/jure) in [#948](https://github.com/apollographql/graphql-tools/pull/948)
* Stop automatically shallow cloning (via object spread syntax) transformed subscription results. Transformed subscription results are not always objects, which means object spreading can lead to invalid results. <br/>
[@ericlewis](https://github.com/ericlewis) in [#928](https://github.com/apollographql/graphql-tools/pull/928)
* Re-use errors with an `extensions` property to make compatible with Apollo Server and it's built-in errors. <br/>
[@edorsey](https://github.com/edorsey) in [#925](https://github.com/apollographql/graphql-tools/pull/925)
* Documentation updates. <br/>
[@Amorites](https://github.com/Amorites) in [#944](https://github.com/apollographql/graphql-tools/pull/944) <br/>
[@trevor-scheer](https://github.com/trevor-scheer) in [#946](https://github.com/apollographql/graphql-tools/pull/946) <br/>
Expand Down
2 changes: 1 addition & 1 deletion src/stitching/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ function concatErrors(errors: Error[]) {
}

function hasResult(error: any) {
return error.result || (error.originalError && error.originalError.result);
return error.result || error.extensions || (error.originalError && error.originalError.result);
}
23 changes: 22 additions & 1 deletion src/test/testErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class ErrorWithResult extends Error {
}
}

class ErrorWithExtensions extends Error {
public extensions: any;
constructor(message: string, code: string) {
super(message);
this.extensions = { code };
}
}

describe('Errors', () => {
describe('getErrorsFromParent', () => {
it('should return OWN error kind if path is not defined', () => {
Expand Down Expand Up @@ -44,6 +52,19 @@ describe('Errors', () => {
}
});

it('persists single error with extensions', () => {
const result = {
errors: [new ErrorWithExtensions('Test error', 'UNAUTHENTICATED')]
};
try {
checkResultAndHandleErrors(result, {} as GraphQLResolveInfo, 'responseKey');
} catch (e) {
assert.equal(e.message, 'Test error');
assert.equal(e.extensions && e.extensions.code, 'UNAUTHENTICATED');
assert.isUndefined(e.originalError.errors);
}
});

it('persists original errors without a result', () => {
const result = {
errors: [new Error('Test error')]
Expand All @@ -61,7 +82,7 @@ describe('Errors', () => {
}
});

it('combines errors and perists the original errors', () => {
it('combines errors and persists the original errors', () => {
const result = {
errors: [new Error('Error1'), new Error('Error2')]
};
Expand Down