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

feat: original error as extensions #1514

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
53 changes: 39 additions & 14 deletions packages/core/src/plugins/use-masked-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,64 @@ export const DEFAULT_ERROR_MESSAGE = 'Unexpected error.';
export type MaskErrorFn = (error: unknown, message: string) => Error;

export type SerializableGraphQLErrorLike = Error & {
name: 'GraphQLError';
toJSON(): { message: string };
toJSON?(): { message: string };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait I just realized this. Why we remove name here? GraphQLError should have GraphQLError name as in the following isGraphQLError check

extensions?: Record<string, unknown>;
};

export function isGraphQLError(error: unknown): error is Error & { originalError?: Error } {
Copy link
Collaborator

@ardatan ardatan Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the return type should be error is SerializableGraphQLErrorLike

return error instanceof Error && error.name === 'GraphQLError';
}

export function createSerializableGraphQLError(message: string): SerializableGraphQLErrorLike {
const error = new Error(message);
function createSerializableGraphQLError(
message: string,
originalError: unknown,
isDev: boolean
): SerializableGraphQLErrorLike {
const error: SerializableGraphQLErrorLike = new Error(message);
error.name = 'GraphQLError';
if (isDev) {
const extensions =
originalError instanceof Error
? { message: originalError.message, stack: originalError.stack }
: { message: String(originalError) };

Object.defineProperty(error, 'extensions', {
get() {
return extensions;
},
});
}

Object.defineProperty(error, 'toJSON', {
value() {
return {
message: error.message,
extensions: error.extensions,
};
},
});

return error as SerializableGraphQLErrorLike;
}

export const defaultMaskErrorFn: MaskErrorFn = (err, message) => {
if (isGraphQLError(err)) {
if (err?.originalError) {
if (isGraphQLError(err.originalError)) {
return err;
export const createDefaultMaskErrorFn =
(isDev: boolean): MaskErrorFn =>
(error, message) => {
if (isGraphQLError(error)) {
if (error?.originalError) {
if (isGraphQLError(error.originalError)) {
return error;
}
return createSerializableGraphQLError(message, error, isDev);
}
return createSerializableGraphQLError(message);
return error;
}
return err;
}
return createSerializableGraphQLError(message);
};
return createSerializableGraphQLError(message, error, isDev);
};

const isDev = globalThis.process?.env?.NODE_ENV === 'development';

export const defaultMaskErrorFn: MaskErrorFn = createDefaultMaskErrorFn(isDev);

export type UseMaskedErrorsOpts = {
/** The function used for identify and mask errors. */
Expand Down
55 changes: 54 additions & 1 deletion packages/core/test/plugins/use-masked-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
collectAsyncIteratorValues,
createTestkit,
} from '@envelop/testing';
import { useMaskedErrors, DEFAULT_ERROR_MESSAGE, MaskErrorFn } from '../../src/plugins/use-masked-errors.js';
import {
useMaskedErrors,
DEFAULT_ERROR_MESSAGE,
MaskErrorFn,
createDefaultMaskErrorFn,
} from '../../src/plugins/use-masked-errors.js';
import { useExtendContext } from '@envelop/core';
import { useAuth0 } from '../../../plugins/auth0/src/index.js';
import { GraphQLError } from 'graphql';
Expand Down Expand Up @@ -427,4 +432,52 @@ describe('useMaskedErrors', () => {
}
expect.assertions(1);
});

it('should include the original error message stack in the extensions in development mode', async () => {
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => {
throw new Error("I'm a teapot");
},
},
},
});
const testInstance = createTestkit([useMaskedErrors({ maskErrorFn: createDefaultMaskErrorFn(true) })], schema);
const result = await testInstance.execute(`query { foo }`, {}, {});
assertSingleExecutionValue(result);
expect(result.errors?.[0].extensions).toEqual({
message: "I'm a teapot",
stack: expect.stringMatching(/^Error: I'm a teapot/),
});
});

it('should include the original thrown thing in the extensions in development mode', async () => {
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => {
throw "I'm a teapot";
},
},
},
});
const testInstance = createTestkit([useMaskedErrors({ maskErrorFn: createDefaultMaskErrorFn(true) })], schema);
const result = await testInstance.execute(`query { foo }`, {}, {});
assertSingleExecutionValue(result);
expect(result.errors?.[0].extensions).toEqual({
message: 'Unexpected error value: "I\'m a teapot"',
stack: expect.stringMatching(/NonErrorThrown: Unexpected error value: \"I'm a teapot/),
});
});
});