{"id":"f3aa1efa9344d369","repo":"expressjs/express","slug":"invalid-status-code-json-stringify-code-stat-f3aa1e","errorCode":null,"errorMessage":"Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.","messagePattern":"Invalid status code: (.+?)\\. Status code must be greater than 99 and less than 1000\\.","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"lib/response.js","lineNumber":72,"sourceCode":" *\n * Expects an integer value between 100 and 999 inclusive.\n * Throws an error if the provided status code is not an integer or if it's outside the allowable range.\n *\n * @param {number} code - The HTTP status code to set.\n * @return {ServerResponse} - Returns itself for chaining methods.\n * @throws {TypeError} If `code` is not an integer.\n * @throws {RangeError} If `code` is outside the range 100 to 999.\n * @public\n */\n\nres.status = function status(code) {\n  // Check if the status code is not an integer\n  if (!Number.isInteger(code)) {\n    throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);\n  }\n  // Check if the status code is outside of Node's valid range\n  if (code < 100 || code > 999) {\n    throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);\n  }\n\n  this.statusCode = code;\n  return this;\n};\n\n/**\n * Set Link header field with the given `links`.\n *\n * Examples:\n *\n *    res.links({\n *      next: 'http://api.example.com/users?page=2',\n *      last: 'http://api.example.com/users?page=5',\n *      pages: [\n *        'http://api.example.com/users?page=1',\n *        'http://api.example.com/users?page=2'\n *      ]","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/expressjs/express/blob/a3714473feb3d2908add734d340e7755fd85e0a3/lib/response.js#L54-L90","documentation":"Thrown by Express's `res.status(code)` when the integer status code is outside the range 100–999 that Node's HTTP layer accepts. Express validates before assigning `this.statusCode` so the failure surfaces at the call site instead of deep inside Node when headers are written. A separate TypeError covers non-integer input; this RangeError covers out-of-range integers.","triggerScenarios":"Calling `res.status(code)` (or `res.sendStatus`) with an integer < 100 or > 999, e.g. `res.status(0)`, `res.status(99)`, `res.status(1000)`, or a computed/derived code that fell out of range.","commonSituations":"Passing an upstream service's error code or exit code straight through as an HTTP status; using a database/application error number as a status; arithmetic on status codes producing 0; forwarding `err.status` that was never set on custom errors after coercion.","solutions":["Validate or clamp the code before calling res.status — map internal error codes to real HTTP statuses (e.g. default to 500).","When proxying upstream responses, only forward `upstream.status` if it is between 100 and 999; otherwise use 502.","If the value comes from `err.status`/`err.statusCode`, fall back: `res.status(Number.isInteger(err.status) && err.status >= 100 && err.status <= 999 ? err.status : 500)`."],"exampleFix":"// before\nres.status(upstreamError.code).send('failed'); // code was 0\n// after\nconst status = Number.isInteger(upstreamError.code) && upstreamError.code >= 100 && upstreamError.code <= 999 ? upstreamError.code : 500;\nres.status(status).send('failed');","handlingStrategy":"validation","validationCode":"const code = Number(status);\nif (!Number.isInteger(code) || code < 100 || code > 999) {\n  throw new Error(`Refusing invalid HTTP status: ${status}`);\n}\nres.status(code).send(body);","typeGuard":"function isValidStatusCode(code) {\n  return Number.isInteger(code) && code >= 100 && code <= 999;\n}","tryCatchPattern":null,"preventionTips":["Never pass user- or upstream-derived values directly to res.status(); map them through an allowlist of statuses your app actually uses","Coerce with Number() and check Number.isInteger before calling res.status","Centralize error-to-status mapping in one helper so the range check lives in exactly one place"],"tags":["express","http","status-code","range-error"],"analyzedSha":"a3714473feb3d2908add734d340e7755fd85e0a3","analyzedAt":"2026-07-31T18:55:36.968Z","schemaVersion":2}