diff --git a/src/middleware/graphql-server/index.test.ts b/src/middleware/graphql-server/index.test.ts index 51974626..4f4f55ec 100644 --- a/src/middleware/graphql-server/index.test.ts +++ b/src/middleware/graphql-server/index.test.ts @@ -8,6 +8,9 @@ import { import { Hono } from '@/hono' import { errorMessages, graphqlServer } from '@/middleware/graphql-server' +// Do not show `console.error` messages +jest.spyOn(console, 'error').mockImplementation() + describe('errorMessages', () => { const messages = errorMessages(['message a', 'message b']) expect(messages).toEqual({ diff --git a/src/middleware/graphql-server/index.ts b/src/middleware/graphql-server/index.ts index 1bc5294b..d0455635 100644 --- a/src/middleware/graphql-server/index.ts +++ b/src/middleware/graphql-server/index.ts @@ -55,6 +55,7 @@ export const graphqlServer = (options: Options) => { params = await getGraphQLParams(c.req) } catch (e) { if (e instanceof Error) { + console.error(`${e.stack || e.message}`) c.res = c.json(errorMessages([e.message], [e]), 400) } return @@ -83,6 +84,7 @@ export const graphqlServer = (options: Options) => { } catch (syntaxError: unknown) { // Return 400: Bad Request if any syntax errors errors exist. if (syntaxError instanceof Error) { + console.error(`${syntaxError.stack || syntaxError.message}`) const e = new GraphQLError(syntaxError.message, { originalError: syntaxError, }) @@ -135,6 +137,7 @@ export const graphqlServer = (options: Options) => { }) } catch (contextError: unknown) { if (contextError instanceof Error) { + console.error(`${contextError.stack || contextError.message}`) const e = new GraphQLError(contextError.message, { originalError: contextError, nodes: documentAST, @@ -237,4 +240,4 @@ export const errorMessages = ( } } -export const graphiQLResponse = () => {} +// export const graphiQLResponse = () => {} diff --git a/src/middleware/graphql-server/parse-body.ts b/src/middleware/graphql-server/parse-body.ts index 02c28392..ea827e67 100644 --- a/src/middleware/graphql-server/parse-body.ts +++ b/src/middleware/graphql-server/parse-body.ts @@ -8,6 +8,9 @@ export async function parseBody(req: Request): Promise<{ [param: string]: unknow try { return await req.json() } catch (e) { + if (e instanceof Error) { + console.error(`${e.stack || e.message}`) + } throw Error(`POST body sent invalid JSON: ${e}`) } case 'application/x-www-form-urlencoded':