Skip to content

Conversation

Vedant224
Copy link

Fixes #6756

This PR adds type validation to res.sendStatus() to prevent uncaught TypeError when BigInt values are passed as status codes.

Problem:

  • res.sendStatus(200n) caused uncaught "Do not know how to serialize a BigInt" error
  • Error occurred when sendStatus() called this.status() which internally uses JSON.stringify()

Solution:

  • Add type checking at the beginning of sendStatus() method
  • Throw consistent TypeError: Invalid status code for non-number inputs
  • Matches existing error handling patterns in Express

Changes:

  • Add type validation in lib/response.js
  • Add test case for BigInt input in test/res.sendStatus.js
  • All existing tests pass (1239 passing)
  • Follows existing error message format
  • No linting errors

Testing:

npm test  # All 1239 tests pass
npm run lint  # No errors

…on error (expressjs#6756)

- Add type checking in sendStatus() method to throw TypeError for non-number inputs
- Prevents uncaught 'Do not know how to serialize a BigInt' error
- Add test coverage for BigInt status code input
- Maintains backward compatibility with existing error patterns

Fixes expressjs#6756

res.sendStatus = function sendStatus(statusCode) {
if (typeof statusCode !== 'number') {
throw new TypeError('Invalid status code: ' + statusCode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new TypeError('Invalid status code: ' + statusCode);
throw new TypeError(`statusCode must be a valid number to res.sendStatus`);

I think it seems consistent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

Copy link
Member

@bjohansebas bjohansebas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, although this will be released for version 6 since it would be a breaking change, and we should first release a warning in version 5. Could you create a new PR to add a deprecation message?

@Vedant224
Copy link
Author

Vedant224 commented Sep 18, 2025

@bjohansebas I've created the deprecation warning PR as requested. I kept it targeting master since that shows my actual 2-file changes cleanly. When I tried changing the target to 5.x it showed 36 changes (difference between branches).

Should I leave it targeting master and you'll handle getting it into Express v5, or would you prefer a different approach for the branch targeting?

The deprecation warning PR is ready for review - it adds the deprecate() call for non-number values in sendStatus().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uncaught TypeError exception when using sendStatus with a BigNum

3 participants