Skip to content

Commit

Permalink
preserve masked errors' path
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Nov 27, 2023
1 parent c79d6ff commit 599f057
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/plugins/use-masked-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,6 +50,7 @@ function createSerializableGraphQLError(
Object.defineProperty(error, 'toJSON', {
value() {
return {
...(hasPath ? {path: originalError.path} : {}),
message: error.message,
extensions: error.extensions,
};
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/plugins/use-masked-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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']);

Check failure on line 165 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / k6

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 165 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / build / graphql v15.8.0

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 165 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / build / graphql v16

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 165 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / esm

Cannot find name 'error'. Did you mean 'err'?
}
});
it('Should properly mask context creation errors', async () => {
Expand All @@ -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']);

Check failure on line 183 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / k6

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 183 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / build / graphql v15.8.0

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 183 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / build / graphql v16

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 183 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / esm

Cannot find name 'error'. Did you mean 'err'?
}
});

Expand All @@ -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']);

Check failure on line 204 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / k6

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 204 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / build / graphql v15.8.0

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 204 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / build / graphql v16

Cannot find name 'error'. Did you mean 'err'?

Check failure on line 204 in packages/core/test/plugins/use-masked-errors.spec.ts

View workflow job for this annotation

GitHub Actions / esm

Cannot find name 'error'. Did you mean 'err'?
} else {
throw err;
}
Expand Down

0 comments on commit 599f057

Please sign in to comment.