Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ module.exports = res
res.status = function status(code) {
// Check if the status code is not an integer
if (!Number.isInteger(code)) {
throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);
// Use String() instead of JSON.stringify() to handle BigInt and other types safely
var stringifiedCode = typeof code === 'bigint' ? code.toString() : JSON.stringify(code)
throw new TypeError(`Invalid status code: ${stringifiedCode}. Status code must be an integer.`);
}
// Check if the status code is outside of Node's valid range
if (code < 100 || code > 999) {
Expand Down
12 changes: 12 additions & 0 deletions test/res.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ describe('res', function () {
.get('/')
.expect(500, /Invalid status code/, done);
});

it('should raise error for BigInt status code with readable message', function (done) {
var app = express();

app.use(function (req, res) {
res.status(200n).end();
});

request(app)
.get('/')
.expect(500, /Invalid status code: 200\. Status code must be an integer/, done);
});
});
});
});
Expand Down