Skip to content

Commit

Permalink
fix: use reply instead of original res (#1704)
Browse files Browse the repository at this point in the history
  • Loading branch information
XVincentX authored and delvedor committed Jun 11, 2019
1 parent b53f037 commit d8730a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/context.js
Expand Up @@ -27,9 +27,9 @@ function Context (schema, handler, Reply, Request, contentTypeParser, config, er
function defaultErrorHandler (error, request, reply) {
var res = reply.res
if (res.statusCode >= 500) {
res.log.error({ req: reply.request.raw, res: res, err: error }, error && error.message)
reply.log.error({ req: reply.request.raw, res: res, err: error }, error && error.message)
} else if (res.statusCode >= 400) {
res.log.info({ res: res, err: error }, error && error.message)
reply.log.info({ res: res, err: error }, error && error.message)
}
reply.send(error)
}
Expand Down
32 changes: 32 additions & 0 deletions test/logger.test.js
Expand Up @@ -1072,6 +1072,38 @@ test('should not log the error if error handler is defined', t => {
})
})

test('should not rely on raw request to log errors', t => {
t.plan(7)
const stream = split(JSON.parse)
const fastify = Fastify({
modifyCoreObjects: false,
logger: {
stream: stream,
level: 'info'
}
})
fastify.get('/error', function (req, reply) {
t.ok(req.log)
reply.status(415).send(new Error('something happened'))
})
fastify.listen(0, err => {
t.error(err)
fastify.server.unref()
http.get('http://localhost:' + fastify.server.address().port + '/error')
stream.once('data', listenAtLogLine => {
t.ok(listenAtLogLine, 'listen at log message is ok')
stream.once('data', line => {
t.equal(line.msg, 'incoming request', 'message is set')
stream.once('data', line => {
t.equal(line.level, 30, 'level is correct')
t.equal(line.msg, 'something happened', 'message is set')
t.deepEqual(line.res, { statusCode: 415 }, 'status code is set')
})
})
})
})
})

test('should redact the authorization header if so specified', t => {
t.plan(7)
const stream = split(JSON.parse)
Expand Down

0 comments on commit d8730a2

Please sign in to comment.