Skip to content

Commit 55e9f2c

Browse files
fix: use the status code as the error body when it has no name (#170)
* fix: use the status code as the error body when it has no name In production, getErrorMessage() fell back to statuses.message[status], which is undefined for a valid error status that has no name, such as 499. The response body was <pre>undefined</pre>. Fall back to the status code itself, as res.sendStatus() does in Express. * ci: pin to supertest 7.3.0 because of streaming request regression in 7.3.1 Ref: forwardemail/supertest#911 --------- Co-authored-by: Phillip Barta <barta.phillip@gmail.com>
1 parent e5890f7 commit 55e9f2c

4 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎HISTORY.md‎

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

4+
* fix: use the status code as the error body when it has no name
45
* deps:
56
* debug@^4.4.3
67
* statuses@^2.0.2

‎index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function getErrorMessage (err, status, env) {
167167
}
168168
}
169169

170-
return msg || statuses.message[status]
170+
return msg || statuses.message[status] || String(status)
171171
}
172172

173173
/**

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"eslint-plugin-standard": "^4.1.0",
2929
"mocha": "^11.0.1",
3030
"nyc": "^17.1.0",
31-
"supertest": "^7.0.0"
31+
"supertest": "7.3.0"
3232
},
3333
"files": [
3434
"index.js"

‎test/test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ var topDescribe = function (type, createServer) {
351351
.expect(501, /<pre>Not Implemented<\/pre>/, done)
352352
})
353353

354+
it('should send status code when production and no status code name', function (done) {
355+
var err = createError('boom!', {
356+
status: 499
357+
})
358+
wrapper(request(createServer(err, {
359+
env: 'production'
360+
}))
361+
.get('/foo'))
362+
.expect(499, /<pre>499<\/pre>/, done)
363+
})
364+
354365
describe('when there is a request body', function () {
355366
it('should not hang/error when unread', function (done) {
356367
var buf = Buffer.alloc(1024 * 16, '.')

0 commit comments

Comments
 (0)