0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00

feat: show stack-trace at graphql-server middleware (#161)

This commit is contained in:
Yusuke Wada 2022-04-20 18:38:46 +09:00 committed by GitHub
parent c9053ebd79
commit d4eb8071db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -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({

View File

@ -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 = () => {}

View File

@ -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':