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

preserve masked errors' path #2096

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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
Loading