sveltejs/kit · error

HTTP error status codes must be between 400 and 599 — ${stat

Error message

HTTP error status codes must be between 400 and 599 — ${status} is invalid

What it means

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.

Source

Thrown at packages/kit/src/exports/index.js:83

 * @return {never}
 * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
 * @throws {Error} If the provided status is invalid (not between 400 and 599).
 */
/**
 * Throws an error with a HTTP status code and an optional message.
 * When called during request handling, this will cause SvelteKit to
 * return an error response; the error will be passed to `handleError` as an _expected_ error.
 * Make sure you're not catching the thrown error, which would prevent SvelteKit from handling it.
 * @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.
 * @param {any} [message] A string, or (deprecated) a partial App.Error object
 * @param {any} [properties] Additional properties of the App.Error type when passing a string message.
 * @return {never}
 * @throws {import('./public.js').HttpError} This error instructs SvelteKit to initiate HTTP error handling.
 * @throws {Error} If the provided status is invalid (not between 400 and 599).
 */
export function error(status, message, properties) {
	if ((!BROWSER || DEV) && (isNaN(status) || status < 400 || status > 599)) {
		throw new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);
	}

	if (message !== undefined && typeof message !== 'string') {
		if (DEV) {
			console.warn(
				'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'
			);
		}

		({ message, ...properties } = message);
	}

	throw new HttpError({ ...properties, status, message: message ?? `Error: ${status}` });
}

/**
 * Checks whether this is an error thrown by {@link error}.
 * @template {number} T

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Pass a literal HTTP error status between 400 and 599, e.g. error(404, 'Not found').
  2. If the status is dynamic, validate or clamp it before calling: const s = Math.min(599, Math.max(400, status | 0)).
  3. Use redirect(3xx, location) instead of error() for redirect statuses.
  4. Check where the status value comes from — undefined/null upstream values produce NaN.

Example fix

// before
throw error(status, 'Something went wrong'); // status may be undefined/302
// after
if (typeof status !== 'number' || status < 400 || status > 599) status = 500;
error(status, 'Something went wrong');
Defensive patterns

Strategy: validation

Validate before calling

function safeErrorStatus(status) {
  return typeof status === 'number' && !Number.isNaN(status) && status >= 400 && status <= 599 ? status : 500;
}
// error(safeErrorStatus(resp?.status), 'Request failed')

Type guard

function isHttpErrorStatus(status) {
  return typeof status === 'number' && Number.isInteger(status) && status >= 400 && status <= 599;
}

Try / catch

try {
  error(status, 'Request failed');
} catch (e) {
  if (e instanceof HttpError) throw e;
  if (e.message.startsWith('HTTP error status codes')) {
    error(500, 'Request failed');
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/47e3ccd8b9d70b55. Report an issue: GitHub.