Skip to content

Commit

Permalink
interop with pino-http by setting res.err instead of calling log.error
Browse files Browse the repository at this point in the history
  • Loading branch information
jstewmon committed Dec 24, 2021
1 parent 8dadc9b commit d138fea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
5 changes: 4 additions & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ function logger (opts, stream) {
wrap(ctx.req, ctx.res)
ctx.log = ctx.request.log = ctx.response.log = ctx.req.log
return next().catch(function (err) {
ctx.log.error({ err })
if (!('err' in ctx.res)) {
ctx.res.err = err
}
throw err
})
}

pino.logger = wrap.logger
return pino
}
21 changes: 9 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,22 @@ test('supports errors in the middleware', function (t) {

app.use((ctx, next) => {
if (ctx.request.url === '/error') {
ctx.body = ''
throw Error('boom!')
ctx.throw(418, 'boom!')
}
return next()
})

dest.once('data', function (line) {
// logging the error:
t.ok(line.req, 'req is defined')
t.equal(line.err.message, 'boom!')
dest.once('data', function (line) {
// logging the 500 response:
t.ok(line.req, 'req is defined')
t.ok(line.err, 'err is defined')
t.equal(line.msg, 'request errored')
t.equal(line.req.method, 'GET', 'method is get')
t.equal(line.res.statusCode, 500, 'statusCode is 500')
t.end()
})
t.ok(line.res, 'res is defined')
t.ok(line.err, 'err is defined')
t.ok(line.err.stack, 'err has stack')
t.equal(line.err.message, 'boom!', 'err message is boom!')
t.equal(line.err.status, 418, 'err status is 418')
t.equal(line.err.statusCode, 418, 'err statusCode is 418')
t.equal(line.msg, 'request errored', 'message is request errored')
t.end()
})
})

Expand Down

0 comments on commit d138fea

Please sign in to comment.