Skip to content

Commit

Permalink
fix: correct signature for MaskError (#2206)
Browse files Browse the repository at this point in the history
* fix: correct signature for MaskError

* Prettier

* Go

* Go

* Update packages/graphql-yoga/__tests__/error-masking.spec.ts

Co-authored-by: Laurin Quast <laurinquast@googlemail.com>

* Update .npmrc

Co-authored-by: Saihajpreet Singh <saihajpreet.singh@gmail.com>

Co-authored-by: Laurin Quast <laurinquast@googlemail.com>
Co-authored-by: Saihajpreet Singh <saihajpreet.singh@gmail.com>
  • Loading branch information
3 people committed Dec 6, 2022
1 parent c229019 commit 26d780c
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-zebras-call.md
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

Correct Mask Error Factory signature
46 changes: 46 additions & 0 deletions packages/graphql-yoga/__tests__/error-masking.spec.ts
@@ -1,3 +1,4 @@
import { inspect } from '@graphql-tools/utils'
import { createGraphQLError, createSchema, createYoga } from 'graphql-yoga'

describe('error masking', () => {
Expand Down Expand Up @@ -422,4 +423,49 @@ describe('error masking', () => {
],
})
})

it('call the custom maskError function with correct parameters', async () => {
const yoga = createYoga({
logging: false,
context: () => {
throw new Error('I like turtles')
},
maskedErrors: {
errorMessage: 'My message',
maskError: (error, message, isDev) => {
return createGraphQLError(
inspect({
errorStr: String(error),
message,
isDev,
}),
)
},
isDev: true,
},
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
a: String!
}
`,
}),
})

const response = await yoga.fetch('http://yoga/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query: '{ __typename }' }),
})
expect(await response.json()).toMatchObject({
errors: [
{
message:
'{ errorStr: "Error: I like turtles", message: "My message", isDev: true }',
},
],
})
})
})
1 change: 1 addition & 0 deletions packages/graphql-yoga/src/error.ts
Expand Up @@ -59,6 +59,7 @@ export function handleError(
const maskedError = maskedErrorsOpts.maskError(
error,
maskedErrorsOpts.errorMessage,
maskedErrorsOpts.isDev,
)

if (maskedError !== error) {
Expand Down
4 changes: 1 addition & 3 deletions packages/graphql-yoga/src/schema.ts
Expand Up @@ -8,7 +8,5 @@ import { GraphQLSchemaWithContext, YogaInitialContext } from './types.js'
export function createSchema<TContext = {}>(
opts: IExecutableSchemaDefinition<TContext & YogaInitialContext>,
): GraphQLSchemaWithContext<TContext> {
return makeExecutableSchema<TContext & YogaInitialContext>({
...opts,
})
return makeExecutableSchema<TContext & YogaInitialContext>(opts)
}
17 changes: 11 additions & 6 deletions packages/graphql-yoga/src/server.ts
Expand Up @@ -15,6 +15,7 @@ import {
FetchAPI,
GraphQLParams,
YogaMaskedErrorOpts,
MaskError,
} from './types.js'
import {
OnParamsHook,
Expand Down Expand Up @@ -221,11 +222,13 @@ export class YogaServer<
? createLogger(logger)
: logger

const maskErrorFn =
const maskErrorFn: MaskError =
(typeof options?.maskedErrors === 'object' &&
options.maskedErrors.maskError) ||
yogaDefaultFormatError

const maskedErrorSet = new WeakSet()

this.maskedErrorsOpts =
options?.maskedErrors === false
? null
Expand All @@ -235,19 +238,21 @@ export class YogaServer<
? options.maskedErrors
: {}),
maskError: (error, message) => {
if (maskedErrorSet.has(error as Error)) {
return error as Error
}
const newError = maskErrorFn(
{
error,
message,
isDev: this.maskedErrorsOpts?.isDev ?? false,
},
error,
message,
this.maskedErrorsOpts?.isDev,
)

if (newError !== error) {
this.logger.error(error)
}

maskedErrorSet.add(newError)

return newError
},
}
Expand Down
8 changes: 7 additions & 1 deletion packages/graphql-yoga/src/types.ts
@@ -1,4 +1,4 @@
import type { MaskError, PromiseOrValue } from '@envelop/core'
import type { PromiseOrValue } from '@envelop/core'
import type { createFetch } from '@whatwg-node/fetch'
import type { GraphQLSchema } from 'graphql'

Expand Down Expand Up @@ -57,4 +57,10 @@ export type YogaMaskedErrorOpts = {
isDev?: boolean
}

export type MaskError = (
error: unknown,
message: string,
isDev?: boolean,
) => Error

export type MaybeArray<T> = T | T[]
21 changes: 8 additions & 13 deletions packages/graphql-yoga/src/utils/yoga-default-format-error.ts
@@ -1,18 +1,13 @@
import { createGraphQLError } from '@graphql-tools/utils'
import { GraphQLErrorExtensions } from 'graphql'
import { isGraphQLError } from '../error.js'
import { MaskError } from '../types.js'

export const yogaDefaultFormatError = ({
error,
message,
isDev,
}: {
error: unknown
message: string
isDev?: boolean
}) => {
const dev = isDev || globalThis.process?.env?.NODE_ENV === 'development'

export const yogaDefaultFormatError: MaskError = (
error: unknown,
message: string,
isDev = globalThis.process?.env?.NODE_ENV === 'development',
) => {
if (isGraphQLError(error)) {
if (error.originalError) {
if (error.originalError.name === 'GraphQLError') {
Expand All @@ -23,7 +18,7 @@ export const yogaDefaultFormatError = ({
...error.extensions,
unexpected: true,
}
if (dev) {
if (isDev) {
extensions.originalError = {
message: error.originalError.message,
stack: error.originalError.stack,
Expand All @@ -43,7 +38,7 @@ export const yogaDefaultFormatError = ({
return createGraphQLError(message, {
extensions: {
unexpected: true,
originalError: dev
originalError: isDev
? error instanceof Error
? {
message: error.message,
Expand Down

0 comments on commit 26d780c

Please sign in to comment.