Handle error from res.statusCode

This commit is contained in:
David Cheung 2016-05-13 11:27:53 -04:00
parent 0158d3a735
commit cfde2a8bb0
2 changed files with 17 additions and 0 deletions

View File

@ -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);

View File

@ -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() {