{"record":{"id":"47e3ccd8b9d70b55","repo":"sveltejs/kit","slug":"http-error-status-codes-must-be-between-400-and-59","errorCode":null,"errorMessage":"HTTP error status codes must be between 400 and 599 — ${status} is invalid","messagePattern":"HTTP error status codes must be between 400 and 599 — (.+?) is invalid","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/exports/index.js","lineNumber":83,"sourceCode":" * @return {never}\n * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.\n * @throws {Error} If the provided status is invalid (not between 400 and 599).\n */\n/**\n * Throws an error with a HTTP status code and an optional message.\n * When called during request handling, this will cause SvelteKit to\n * return an error response; the error will be passed to `handleError` as an _expected_ error.\n * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.\n * @param {any} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599.\n * @param {any} [message] A string, or (deprecated) a partial App.Error object\n * @param {any} [properties] Additional properties of the App.Error type when passing a string message.\n * @return {never}\n * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.\n * @throws {Error} If the provided status is invalid (not between 400 and 599).\n */\nexport function error(status, message, properties) {\n\tif ((!BROWSER || DEV) && (isNaN(status) || status < 400 || status > 599)) {\n\t\tthrow new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);\n\t}\n\n\tif (message !== undefined && typeof message !== 'string') {\n\t\tif (DEV) {\n\t\t\tconsole.warn(\n\t\t\t\t'Passing an `App.Error` body as the second argument is deprecated — pass the `message` as the second argument, and any additional properties as the third'\n\t\t\t);\n\t\t}\n\n\t\t({ message, ...properties } = message);\n\t}\n\n\tthrow new HttpError({ ...properties, status, message: message ?? `Error: ${status}` });\n}\n\n/**\n * Checks whether this is an error thrown by {@link error}.\n * @template {number} T","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/exports/index.js#L65-L101","documentation":"The `error(status, ...)` helper throws an HttpError for SvelteKit's error handling, and its status must be a valid HTTP error code between 400 and 599. On the server or in dev, calling error() with NaN or a status outside that range throws this plain Error instead, alerting you to a bug in your code rather than emitting a bogus HTTP response.","triggerScenarios":"Calling error(variable) where variable is undefined/NaN, or explicit calls like error(200, 'nope'), error(302), error(999) in load functions, actions, or hooks.","commonSituations":"Deriving the status from a response object that is undefined; confusing error() with redirect() and passing 3xx codes; hand-rolling status mapping tables with invalid values; passing a string like '404' that becomes NaN checks failing via isNaN.","solutions":["Pass a literal HTTP error status between 400 and 599, e.g. error(404, 'Not found').","If the status is dynamic, validate or clamp it before calling: const s = Math.min(599, Math.max(400, status | 0)).","Use redirect(3xx, location) instead of error() for redirect statuses.","Check where the status value comes from — undefined/null upstream values produce NaN."],"exampleFix":"// before\nthrow error(status, 'Something went wrong'); // status may be undefined/302\n// after\nif (typeof status !== 'number' || status < 400 || status > 599) status = 500;\nerror(status, 'Something went wrong');","handlingStrategy":"validation","validationCode":"function safeErrorStatus(status) {\n  return typeof status === 'number' && !Number.isNaN(status) && status >= 400 && status <= 599 ? status : 500;\n}\n// error(safeErrorStatus(resp?.status), 'Request failed')","typeGuard":"function isHttpErrorStatus(status) {\n  return typeof status === 'number' && Number.isInteger(status) && status >= 400 && status <= 599;\n}","tryCatchPattern":"try {\n  error(status, 'Request failed');\n} catch (e) {\n  if (e instanceof HttpError) throw e;\n  if (e.message.startsWith('HTTP error status codes')) {\n    error(500, 'Request failed');\n  }\n  throw e;\n}","preventionTips":["Always pass literal 4xx/5xx constants to error() where possible.","Validate dynamic statuses (from fetch responses) before passing them to error().","Use redirect() for 3xx codes; never pass them to error().","Wrap upstream status extraction (resp?.status) with a fallback of 500."],"tags":["sveltekit","http-status","error-handling","validation"],"backgroundTag":"invalid-http-status-code","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}