Skip to content

Commit d2a6a26

Browse files
committed
fix: handle NaN status codes by defaulting to 500
Previously, when createError(NaN) was called, the status validation would fail to catch NaN because typeof NaN === 'number' is true in JavaScript. This resulted in errors with NaN status codes, which could cause issues in downstream code. This fix adds an explicit isNaN() check to the status validation logic, ensuring that NaN status codes are properly caught and defaulted to 500. Additionally, a test case has been added to verify this behavior and prevent regression. Fixes: NaN status codes not being validated correctly
1 parent 61aee57 commit d2a6a26

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function createError () {
7373
deprecate('non-error status code; use only 4xx or 5xx status codes')
7474
}
7575

76-
if (typeof status !== 'number' ||
76+
if (typeof status !== 'number' || isNaN(status) ||
7777
(!statuses.message[status] && (status < 400 || status >= 600))) {
7878
status = 500
7979
}

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ describe('HTTP Errors', function () {
331331
assert.strictEqual(err.expose, false)
332332
})
333333

334+
it('createError(NaN) should default to 500', function () {
335+
var err = createError(NaN)
336+
assert.strictEqual(err.name, 'InternalServerError')
337+
assert.strictEqual(err.message, 'Internal Server Error')
338+
assert.strictEqual(err.status, 500)
339+
assert.strictEqual(err.statusCode, 500)
340+
assert.strictEqual(err.expose, false)
341+
})
342+
334343
it('createError(err, props)', function () {
335344
var _err = new Error('LOL')
336345
_err.status = 404

0 commit comments

Comments
 (0)