Skip to content

Commit

Permalink
Change non-numeric status code to 500
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 15, 2016
1 parent ab539c3 commit 360c1a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
unreleased
==========

* Change invalid status code to 500
* Change invalid or non-numeric status code to 500
* Overwrite status message to match set status code
* Set response headers from `err.headers` object
* Use `statuses` instead of `http` module for status messages
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function finalhandler (req, res, options) {
}

// default status code to 500 if outside valid range
if (!status || status < 400 || status > 599) {
if (typeof status !== 'number' || status < 400 || status > 599) {
status = 500
}

Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ describe('finalhandler(req, res)', function () {
.get('/')
.expect(500, done)
})

it('should ignore non-numeric err.status', function (done) {
request(createServer(createError('oops', {
status: 'oh no'
})))
.get('/')
.expect(500, done)
})
})

describeStatusMessage('status message', function () {
Expand Down Expand Up @@ -284,6 +292,18 @@ describe('finalhandler(req, res)', function () {
.expect(503, done)
})

it('should convert to 500 is not a number', function (done) {
var server = http.createServer(function (req, res) {
var done = finalhandler(req, res)
res.statusCode = 'oh no'
done(new Error('oops'))
})

request(server)
.get('/foo')
.expect(500, done)
})

it('should override with err.status', function (done) {
var server = http.createServer(function (req, res) {
var done = finalhandler(req, res)
Expand Down

0 comments on commit 360c1a8

Please sign in to comment.