Skip to content

Commit

Permalink
fix: use error message as log message for request error logs (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriVanHoudt authored Mar 21, 2022
1 parent 3399afe commit 3974180
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async function register (server, options) {
tags: event.tags,
err: event.error
},
'request error'
event.error.message // Will default to `Internal Server Error` by hapi
)
} else if (event.channel === 'app' && !isCustomTagsLoggingIgnored(event, ignoredEventTags.request)) {
logEvent(request.logger, event)
Expand Down
34 changes: 33 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,39 @@ experiment('logs each request', () => {
if (count === 0) {
expect(data.err.message).to.equal('boom')
expect(data.level).to.equal(50)
expect(data.msg).to.equal('request error')
expect(data.msg).to.equal('boom')
} else {
expect(data.res.statusCode).to.equal(500)
expect(data.level).to.equal(30)
expect(data.msg).to.match(/get \/ 500 \(\d*ms\)/)
done()
}
count++
cb()
})
await server.inject('/')
await finish
})

test('handles 500s with no message', async () => {
const server = getServer()
let count = 0
let done
const finish = new Promise(function (resolve, reject) {
done = resolve
})
server.route({
path: '/',
method: 'GET',
handler: (req, reply) => {
throw new Error()
}
})
await registerWithSink(server, 'info', (data, enc, cb) => {
if (count === 0) {
expect(data.err.message).to.equal('Internal Server Error')
expect(data.level).to.equal(50)
expect(data.msg).to.equal('Internal Server Error')
} else {
expect(data.res.statusCode).to.equal(500)
expect(data.level).to.equal(30)
Expand Down

0 comments on commit 3974180

Please sign in to comment.