{"id":"0f00a944d7a1f9f6","repo":"expressjs/express","slug":"invalid-status-code-json-stringify-code-stat","errorCode":null,"errorMessage":"Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.","messagePattern":"Invalid status code: (.+?)\\. Status code must be an integer\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/response.js","lineNumber":68,"sourceCode":"module.exports = res\n\n/**\n * Set the HTTP status code for the response.\n *\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',","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/expressjs/express/blob/a3714473feb3d2908add734d340e7755fd85e0a3/lib/response.js#L50-L86","documentation":"res.status(code) in Express 5 strictly validates its argument: lib/response.js:67-68 throws a TypeError if the code is not an integer (a separate RangeError at line 71-72 covers integers outside 100-999). Express 4 silently coerced values like '200' or floats; Express 5 made this a hard error, so the JSON.stringify in the message shows you exactly what invalid value arrived.","triggerScenarios":"res.status('404') or res.status(`${code}`) — numeric strings are no longer accepted; res.status(undefined) when an upstream error object lacks .status/.statusCode; res.status(err.statusCode) where the property is a string set by a proxy or older library; res.status(200.5) or NaN from arithmetic.","commonSituations":"Express 4→5 migration is the dominant cause: code that passed string status codes worked for years and breaks on upgrade; error-handler middleware doing res.status(err.status) where err.status is undefined for plain Errors; status codes read from route params, JSON payloads, or env vars without Number conversion.","solutions":["Pass an integer: res.status(404), or convert explicitly with Number.parseInt(value, 10) and validate the result.","In error handlers, default missing codes: res.status(Number.isInteger(err.status) ? err.status : 500).","Audit call sites after an Express 4→5 upgrade for string status codes (the JSON.stringify output in the message tells you whether you got a string, undefined, or a float)."],"exampleFix":"// before (worked in Express 4)\nres.status(err.status || '500').send(err.message);\n// after\nconst code = Number.isInteger(err.status) ? err.status : 500;\nres.status(code).send(err.message);","handlingStrategy":"type-guard","validationCode":"if (!Number.isInteger(code) || code < 100 || code > 599) {\n  throw new RangeError(`invalid HTTP status code: ${JSON.stringify(code)}`);\n}\nres.status(code);","typeGuard":"function isValidStatusCode(code) {\n  return Number.isInteger(code) && code >= 100 && code <= 599;\n}","tryCatchPattern":"try {\n  res.status(upstream.statusCode).send(body);\n} catch (err) {\n  if (/Invalid status code/.test(err.message)) {\n    // non-integer (string, undefined, float) reached res.status — normalize and fall back to 502/500\n    return res.status(502).send('Bad upstream status');\n  }\n  throw err;\n}","preventionTips":["Coerce statuses from external sources (upstream APIs, DB rows, env vars) with Number() and validate with Number.isInteger before res.status.","Common trap: err.status may be undefined or a string — use res.status(Number.isInteger(err.status) ? err.status : 500) in error handlers.","Prefer named constants (e.g. http-status-codes or a local enum) over hand-typed numbers to avoid '404' vs 404 mistakes.","Cover error-handler paths in tests with errors lacking a numeric status property."],"tags":["express","http-status","typeerror","express-5","migration"],"analyzedSha":"a3714473feb3d2908add734d340e7755fd85e0a3","analyzedAt":"2026-07-31T18:55:36.968Z","schemaVersion":2}