Skip to content

Commit

Permalink
Do not swallow errors after parsing JSON body
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Dec 5, 2017
1 parent 27c184d commit 01f1039
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/handleRequest.js
Expand Up @@ -63,11 +63,14 @@ function jsonBody (req, res, params, context) {
setImmediate(parse, body)
}
function parse (json) {
var parsed
try {
jsonBodyParsed(null, JSON.parse(body), req, res, params, context)
parsed = JSON.parse(body)
} catch (err) {
jsonBodyParsed(err, null, req, res, params, context)
return
}
jsonBodyParsed(null, parsed, req, res, params, context)
}
}

Expand Down
36 changes: 36 additions & 0 deletions test/error-in-post.js
@@ -0,0 +1,36 @@
'use strict'

const t = require('tap')
const Fastify = require('..')
const fastify = Fastify()

let errored = false

fastify.route({
method: 'POST',
path: '/jsonBody',
handler: function (req, reply) {
throw new Error('kaboom')
}
})

const reqOpts = {
method: 'POST',
url: '/jsonBody',
payload: {
hello: 'world'
}
}

process.on('uncaughtException', (err) => {
errored = true
t.equal(err.message, 'kaboom')
})

fastify.inject(reqOpts, (res) => {
t.fail('should not be called')
})

process.on('beforeExit', () => {
t.ok(errored)
})

0 comments on commit 01f1039

Please sign in to comment.