Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interop with pino-http by setting res.err instead of calling log.error #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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