Skip to content

Conversation

@theaniketgiri
Copy link

Fixes #6756

Previously, passing a BigInt to res.status() would cause an uncaught TypeError because JSON.stringify() cannot serialize BigInt values. The error occurred when trying to create the error message.

Changes:

  • Replace JSON.stringify(code) with template literal and typeof
  • Error message now shows both the value and its type
  • Add comprehensive tests for BigInt and other invalid types

Fixes expressjs#6756

Previously, passing a BigInt to res.status() would cause an uncaught
TypeError because JSON.stringify() cannot serialize BigInt values.
The error occurred when trying to create the error message.

Changes:
- Replace JSON.stringify(code) with template literal and typeof
- Error message now shows both the value and its type
- Add comprehensive tests for BigInt and other invalid types
@krzysdz
Copy link
Contributor

krzysdz commented Dec 4, 2025

This is a duplicate of #6848, with the same approach.

- Use Object.prototype.toString.call() for null-prototype objects
- String() works safely for primitives like BigInt
- Avoids template literals that would crash on null-prototype objects
- Added test case for Object.create(null) to prove the fix

This improves upon the initial fix by handling the edge case mentioned
in PR expressjs#6848 where Object.create(null) objects have no toString method.
@theaniketgiri
Copy link
Author

theaniketgiri commented Dec 4, 2025

This is a duplicate of #6848, with the same approach.

Thanks for pointing out the Object.create(null) edge case!

I've updated this PR to handle it properly. The fix now uses:

  • Object.prototype.toString.call(code) for objects (handles null-prototype objects safely)
  • String(code) for primitives (handles BigInt correctly)

Added a test case specifically for Object.create(null) to verify the fix.

All 13 tests pass

@prasad-prince

This comment was marked as off-topic.

@jonchurch
Copy link
Member

There's no crash here. Im sorry but misunderstanding the problem you're attempting to solve is a close from me.

@jonchurch jonchurch closed this Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: Do not know how to serialize a BigInt when using sendStatus with a BigNum instead of intended error message

4 participants