From 8bc3caeb37a0306b3eaad7413b8fbbef7043cb87 Mon Sep 17 00:00:00 2001 From: greymoth <246701683+greymoth-jp@users.noreply.github.com> Date: Tue, 30 Jun 2026 15:55:43 +0900 Subject: [PATCH] fix: normalize NaN status to 500 in createError --- index.js | 2 +- test/test.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 82271f6..951a698 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,7 @@ function createError () { deprecate('non-error status code; use only 4xx or 5xx status codes') } - if (typeof status !== 'number' || + if (typeof status !== 'number' || isNaN(status) || (!statuses.message[status] && (status < 400 || status >= 600))) { status = 500 } diff --git a/test/test.js b/test/test.js index 7db9f16..9f11fa1 100644 --- a/test/test.js +++ b/test/test.js @@ -138,6 +138,28 @@ describe('createError(status)', function () { assert.strictEqual(this.error.statusCode, 599) }) }) + + describe('when status is NaN', function () { + before(function () { + this.error = createError(NaN) + }) + + it('should have "message" property of "Internal Server Error"', function () { + assert.strictEqual(this.error.message, 'Internal Server Error') + }) + + it('should have "name" property of "InternalServerError"', function () { + assert.strictEqual(this.error.name, 'InternalServerError') + }) + + it('should have "status" property of 500', function () { + assert.strictEqual(this.error.status, 500) + }) + + it('should have "statusCode" property of 500', function () { + assert.strictEqual(this.error.statusCode, 500) + }) + }) }) describe('createError(status, message)', function () {