Skip to content

fix: use safe stringification for status code error messages#7107

Open
AkaHarshit wants to merge 1 commit intoexpressjs:masterfrom
AkaHarshit:fix/status-code-bigint-error-message
Open

fix: use safe stringification for status code error messages#7107
AkaHarshit wants to merge 1 commit intoexpressjs:masterfrom
AkaHarshit:fix/status-code-bigint-error-message

Conversation

@AkaHarshit
Copy link
Contributor

Problem

Calling res.status(200n) (BigInt) or res.sendStatus(200n) throws an unexpected TypeError: Do not know how to serialize a BigInt instead of the intended Invalid status code error message.

This happens because Number.isInteger(200n) returns false, triggering the error path which uses JSON.stringify(code) in a template literal — but JSON.stringify cannot serialize BigInt and throws its own error first, overriding the intended message.

Similarly, Symbol values would also crash JSON.stringify, and Object.create(null) would crash String().

Solution

Replaced JSON.stringify(code) with a safe stringifyStatusCode() helper function that handles all edge cases:

  • BigIntString(200n)"200" (was JSON.stringify crash)
  • Symbolsymbol.toString()"Symbol(foo)" (was String() crash)
  • Object.create(null)Object.prototype.toString.call()"[object Object]" (was .toString() crash)
  • Strings, booleans, null, undefinedString() works safely

Why it matters

The current behavior confuses developers — they see a serialization error instead of the clear "Invalid status code" message that Express intends to show. This fix ensures all invalid status code types produce the intended, helpful error message.

Testing

Added 4 new test cases to test/res.status.js:

  • BigInt status code (200n)
  • Symbol status code (Symbol('test')))
  • Object status code ({})
  • Boolean status code (true)

Full test suite: 1251 passing, 0 failing, no regressions.

Linked Issue

Fixes: #6756

Replace JSON.stringify(code) with a safe stringifyStatusCode() helper
in res.status() to prevent TypeError when non-serializable values
like BigInt or Symbol are passed as status codes.

JSON.stringify(BigInt(200)) throws 'TypeError: Do not know how to
serialize a BigInt', which overrides the intended 'Invalid status code'
error message. The new helper handles BigInt, Symbol, null-prototype
objects, and all other types safely.

Fixes: expressjs#6756
@AkaHarshit
Copy link
Contributor Author

Hi @wesleytodd, the CI tests have officially passed! ✅ This PR correctly introduces fallback stringification for objects, symbols, and BigInts when an invalid status code is thrown, preventing the unhandled TypeError crashes discussed in the issue. The new tests verifying the behavior have been added and are green. Please let me know if you need any adjustments before merging!
Thanks for your time.

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

1 participant