From cfde2a8bb0b8b08ce1bcace7618b7d571272607a Mon Sep 17 00:00:00 2001 From: David Cheung Date: Fri, 13 May 2016 11:27:53 -0400 Subject: [PATCH] Handle error from res.statusCode --- lib/handler.js | 4 ++++ test/handler.test.js | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/handler.js b/lib/handler.js index 9859eed..bba6ef7 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -42,6 +42,10 @@ exports = module.exports = function createStrongErrorHandler(options) { return req.socket.destroy(); } + // this will alter the err object, to handle when res.statusCode is an error + if (!err.status && !err.statusCode && res.statusCode >= 400) + err.statusCode = res.statusCode; + var data = buildResponseData(err, isDebugMode); debug('Response status %s data %j', data.statusCode, data); diff --git a/test/handler.test.js b/test/handler.test.js index d28f77b..a4bda3c 100644 --- a/test/handler.test.js +++ b/test/handler.test.js @@ -65,6 +65,19 @@ describe('strong-error-handler', function() { request.get('/').expect(400, done); }); + + it('handles error from `res.statusCode`', function(done) { + givenErrorHandlerForError(); + var handler = _requestHandler; + _requestHandler = function(req, res, next) { + res.statusCode = 507; + handler(req, res, next); + }; + request.get('/').expect( + 507, + {error: {statusCode: 507, message: 'Insufficient Storage'}}, + done); + }); }); context('logging', function() {