From d138fea73b0a641a426e28623b8b45ddd727bed4 Mon Sep 17 00:00:00 2001 From: Jonathan Stewmon Date: Wed, 3 Apr 2019 15:31:39 -0500 Subject: [PATCH] interop with pino-http by setting res.err instead of calling log.error --- logger.js | 5 ++++- test.js | 21 +++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/logger.js b/logger.js index ea382a4..7c9a07b 100644 --- a/logger.js +++ b/logger.js @@ -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 } diff --git a/test.js b/test.js index 128559d..f552eb1 100644 --- a/test.js +++ b/test.js @@ -155,8 +155,7 @@ 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() }) @@ -164,16 +163,14 @@ test('supports errors in the middleware', function (t) { 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() }) })