From 599f05735c039feae921abf6101de71687fa76ca Mon Sep 17 00:00:00 2001 From: Bozhidar Hristov Date: Mon, 27 Nov 2023 14:01:26 +0200 Subject: [PATCH] preserve masked errors' path --- packages/core/src/plugins/use-masked-errors.ts | 3 +++ packages/core/test/plugins/use-masked-errors.spec.ts | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/packages/core/src/plugins/use-masked-errors.ts b/packages/core/src/plugins/use-masked-errors.ts index 05bcab4b1..dc4fa49f0 100644 --- a/packages/core/src/plugins/use-masked-errors.ts +++ b/packages/core/src/plugins/use-masked-errors.ts @@ -32,6 +32,8 @@ function createSerializableGraphQLError( ): SerializableGraphQLErrorLike { const error = new Error(message) as SerializableGraphQLErrorLike; error.name = 'GraphQLError'; + + const hasPath = originalError && typeof originalError === 'object' && 'path' in originalError; if (isDev) { const extensions = originalError instanceof Error @@ -48,6 +50,7 @@ function createSerializableGraphQLError( Object.defineProperty(error, 'toJSON', { value() { return { + ...(hasPath ? {path: originalError.path} : {}), message: error.message, extensions: error.extensions, }; diff --git a/packages/core/test/plugins/use-masked-errors.spec.ts b/packages/core/test/plugins/use-masked-errors.spec.ts index 32b552d2e..3879acb4f 100644 --- a/packages/core/test/plugins/use-masked-errors.spec.ts +++ b/packages/core/test/plugins/use-masked-errors.spec.ts @@ -105,6 +105,7 @@ describe('useMaskedErrors', () => { expect(result.errors).toHaveLength(1); const [error] = result.errors!; expect(error.message).toEqual(DEFAULT_ERROR_MESSAGE); + expect(error.path).toEqual(['secret']); }); it('Should not mask expected errors', async () => { @@ -116,6 +117,7 @@ describe('useMaskedErrors', () => { const [error] = result.errors!; expect(error.message).toEqual('This message goes to all the clients out there!'); expect(error.extensions).toEqual({ foo: 1 }); + expect(error.path).toEqual(['secretEnvelop']); }); it('Should not mask GraphQL operation syntax errors (of course it does not since we are only hooking in after execute, but just to be sure)', async () => { @@ -142,6 +144,7 @@ describe('useMaskedErrors', () => { expect(JSON.stringify(result)).toMatchInlineSnapshot( `"{"errors":[{"message":"This message goes to all the clients out there!","locations":[{"line":1,"column":9}],"path":["secretWithExtensions"],"extensions":{"code":"Foo","message":"Bar"}}],"data":null}"`, ); + expect(error.path).toEqual(['secretWithExtensions']); }); it('Should properly mask context creation errors with a custom error message', async () => { @@ -159,6 +162,7 @@ describe('useMaskedErrors', () => { await testInstance.execute(`query { secretWithExtensions }`); } catch (err) { expect(err).toMatchInlineSnapshot(`[GraphQLError: My Custom Error Message.]`); + expect(error.path).toEqual(['secretWithExtensions']); } }); it('Should properly mask context creation errors', async () => { @@ -176,6 +180,7 @@ describe('useMaskedErrors', () => { await testInstance.execute(`query { secretWithExtensions }`); } catch (err: any) { expect(err.message).toEqual(DEFAULT_ERROR_MESSAGE); + expect(error.path).toEqual(['secretWithExtensions']); } }); @@ -196,6 +201,7 @@ describe('useMaskedErrors', () => { if (err instanceof GraphQLError) { expect(err.message).toEqual(`No context for you!`); expect(err.extensions).toEqual({ foo: 1 }); + expect(error.path).toEqual(['secretWithExtensions']); } else { throw err; }