Skip to content

Commit 1997aad

Browse files
committed
Ignore err.status with non-error code
closes #1
1 parent d5938e6 commit 1997aad

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ unreleased
33

44
* Add `stacktrace` option
55
* Add `text/plain` fallback response
6+
* Ignore `err.status` with non-error code
67
* Remove `env` option; use `stacktrace` option instead
78
* Send complete HTML document
89
* Set `X-Content-Type-Options: nosniff` header

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var finalhandler = require('finalhandler')
2323
Returns function to be invoked as the final step for the given `req` and `res`.
2424
This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will
2525
write out a 404 response to the `res`. If it is truthy, an error response will
26-
be written out to the `res`, and `res.statusCode` is set from `err.status`.
26+
be written out to the `res`, and `res.statusCode` is set from `err.status` if the
27+
value is 4xx or 5xx.
2728

2829
#### options.onerror
2930

index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,30 @@ function finalhandler(req, res, options) {
5151
var body
5252
var constructBody
5353
var msg
54+
var status = res.statusCode
5455

5556
// unhandled error
5657
if (err) {
5758
// default status code to 500
58-
if (!res.statusCode || res.statusCode < 400) {
59-
res.statusCode = 500
59+
if (!status || status < 400) {
60+
status = 500
6061
}
6162

6263
// respect err.status
63-
if (err.status) {
64-
res.statusCode = err.status
64+
if (err.status >= 400 && err.status < 600) {
65+
status = err.status
6566
}
6667

6768
// production gets a basic error message
6869
msg = stacktrace
6970
? err.stack || String(err)
70-
: http.STATUS_CODES[res.statusCode]
71+
: http.STATUS_CODES[status]
7172
} else {
72-
res.statusCode = 404
73+
status = 404
7374
msg = 'Cannot ' + req.method + ' ' + (req.originalUrl || req.url)
7475
}
7576

76-
debug('default %s', res.statusCode)
77+
debug('default %s', status)
7778

7879
// schedule onerror callback
7980
if (err && onerror) {
@@ -101,12 +102,13 @@ function finalhandler(req, res, options) {
101102
}
102103

103104
// construct body
104-
body = constructBody(res.statusCode, msg)
105+
body = constructBody(status, msg)
105106

106107
// security header for content sniffing
107108
res.setHeader('X-Content-Type-Options', 'nosniff')
108109

109110
// standard headers
111+
res.statusCode = status
110112
res.setHeader('Content-Type', body.type)
111113
res.setHeader('Content-Length', body.length)
112114

test/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ describe('finalhandler(req, res)', function () {
2828
.get('/')
2929
.expect(400, done)
3030
})
31+
32+
it('should ignore non-error err.status code', function (done) {
33+
var err = new Error()
34+
err.status = 201
35+
var server = createServer(err)
36+
request(server)
37+
.get('/')
38+
.expect(500, done)
39+
})
40+
41+
it('should ignore weird err.status', function (done) {
42+
var err = new Error()
43+
err.status = 'oh no'
44+
var server = createServer(err)
45+
request(server)
46+
.get('/')
47+
.expect(500, done)
48+
})
3149
})
3250

3351
describe('404 response', function () {

0 commit comments

Comments
 (0)