Skip to content

fix: handle BigInt status codes in res.status() and res.sendStatus()#7130

Open
JivinSardine wants to merge 1 commit intoexpressjs:masterfrom
JivinSardine:fix/bigint-status-handling
Open

fix: handle BigInt status codes in res.status() and res.sendStatus()#7130
JivinSardine wants to merge 1 commit intoexpressjs:masterfrom
JivinSardine:fix/bigint-status-handling

Conversation

@JivinSardine
Copy link

Description

This PR fixes a bug where passing a BigInt value (e.g., 200n) to res.status() or res.sendStatus() would throw a TypeError: Do not know how to serialize a BigInt.

Root Cause

The res.status() function uses JSON.stringify(code) in its error message. When a BigInt is passed, JSON.stringify() throws because BigInt values cannot be serialized to JSON.

Fix

Coerce BigInt values to Number before validation. HTTP status codes (100-999) are well within the safe range for JavaScript numbers.

Changes

  • lib/response.js: Add BigInt to Number coercion at the start of res.status()
  • test/res.status.js: Add test for BigInt status code acceptance
  • test/res.sendStatus.js: Add test for BigInt status code with sendStatus

Test Results

All 1251 existing tests pass, plus 2 new BigInt-specific tests.

Fixes #6756

Coerce BigInt to Number for compatibility with HTTP status codes.

Fixes: TypeError when passing BigInt (e.g. 200n) to res.status() or
res.sendStatus(), because JSON.stringify() cannot serialize BigInt values.

Now BigInt status codes like 200n are automatically converted to 200
for proper HTTP status handling.
@JivinSardine JivinSardine force-pushed the fix/bigint-status-handling branch from 7cbf450 to e7b4752 Compare March 26, 2026 08:21
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