Skip to content

Commit

Permalink
Fix regression in handling badly formed JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Dec 1, 2021
1 parent a85dd55 commit 732b2f8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,27 @@ module.exports = async function (app, opts) {
const errorFormatter = typeof opts.errorFormatter === 'function' ? opts.errorFormatter : defaultErrorFormatter

if (typeof opts.errorHandler === 'function') {
app.setErrorHandler(opts.errorHandler)
app.setErrorHandler((error, request, reply) => {
const errorHandler = opts.errorHandler
if (!request[kRequestContext]) {
// Generate the context for this request
request[kRequestContext] = { reply, app }
}

return errorHandler(error, request, reply)
})
} else if (opts.errorHandler === true || opts.errorHandler === undefined) {
app.setErrorHandler((error, request, reply) => {
if (!request[kRequestContext]) {
// Generate the context for this request
request[kRequestContext] = { reply, app }
}

const { statusCode, response } = errorFormatter(
error,
request[kRequestContext]
)
reply.code(statusCode).send(response)
return reply.code(statusCode).send(response)
})
}
const contextFn = opts.context
Expand Down
87 changes: 87 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,90 @@ test('errors - should override statusCode to 200 if the data is present', async

t.equal(res.statusCode, 200)
})

test('bad json', async (t) => {
const schema = `
type Query {
successful: String
}
`

const resolvers = {
Query: {
successful () {
t.fail('Should not be called')
return 'Runs OK'
}
}
}

const app = Fastify()

app.register(GQL, {
schema,
resolvers
})

await app.ready()

const res = await app.inject({
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: 'this is not a json',
url: '/graphql'
})

t.equal(res.statusCode, 400)
t.same(res.json(),
{ data: null, errors: [{ message: 'Unexpected token h in JSON at position 1' }] }
)
})

test('bad json with custom error handler', async (t) => {
t.plan(3)
const schema = `
type Query {
successful: String
}
`

const resolvers = {
Query: {
successful () {
t.fail('Should not be called')
return 'Runs OK'
}
}
}

const app = Fastify()

app.register(GQL, {
schema,
resolvers,
errorHandler: (_, request, reply) => {
t.pass('custom error handler called')
reply.code(400).send({
is: 'error'
})
}
})

await app.ready()

const res = await app.inject({
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: 'this is not a json',
url: '/graphql'
})

t.equal(res.statusCode, 400)
t.same(res.json(), {
is: 'error'
})
})

0 comments on commit 732b2f8

Please sign in to comment.