From b67314e90a1ccac801653ff3e62c4deaeef837bd Mon Sep 17 00:00:00 2001 From: Eric Dorsey Date: Sat, 11 Aug 2018 22:42:00 -0500 Subject: [PATCH 1/4] add test --- src/test/testErrors.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/test/testErrors.ts b/src/test/testErrors.ts index 40c20f7c284..c89ca0b7daf 100644 --- a/src/test/testErrors.ts +++ b/src/test/testErrors.ts @@ -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', () => { @@ -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')] @@ -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')] }; From b9a730c22eefdeb21f1c7d47b2b57009a456aac3 Mon Sep 17 00:00:00 2001 From: Eric Dorsey Date: Sat, 11 Aug 2018 22:42:06 -0500 Subject: [PATCH 2/4] make test pass --- src/stitching/errors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stitching/errors.ts b/src/stitching/errors.ts index e9dd4203394..952c5cd65a8 100644 --- a/src/stitching/errors.ts +++ b/src/stitching/errors.ts @@ -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); } From 6dfe9384317cf041875e79f76702f451458f574d Mon Sep 17 00:00:00 2001 From: Eric Dorsey Date: Sat, 11 Aug 2018 22:48:23 -0500 Subject: [PATCH 3/4] add changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8825b8985ca..3709c962dfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### vNEXT -* ... +* Re-use errors with they contain an extensions property to make compatible with Apollo Server and it's built-in errors. [PR #925](https://github.com/apollographql/graphql-tools/pull/925) ### v3.1.1 From f1439643a3e1c084777f9de86f84c71cfc4ae1f2 Mon Sep 17 00:00:00 2001 From: Eric Dorsey Date: Sun, 12 Aug 2018 16:05:56 -0500 Subject: [PATCH 4/4] fix changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3709c962dfb..2d405521e09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### vNEXT -* Re-use errors with they contain an extensions property to make compatible with Apollo Server and it's built-in errors. [PR #925](https://github.com/apollographql/graphql-tools/pull/925) +* Re-use errors with an extensions property to make compatible with Apollo Server and it's built-in errors. [PR #925](https://github.com/apollographql/graphql-tools/pull/925) ### v3.1.1