@sveltejs/kit module exports core utilities for building SvelteKit applications, including error handling, redirects, and response helpers.
error
Throws an error with an HTTP status code and optional message. When called during request handling, this will cause SvelteKit to return an error response without invokinghandleError.
The HTTP status code. Must be in the range 400-599.
An object that conforms to the App.Error type. If a string is passed, it will be used as the message property.
This function never returns - it always throws.
isHttpError
Checks whether an error was thrown by theerror function.
The error object to check.
Optional status code to filter for. If provided, the function will only return true if the error matches this specific status.
Returns
true if the error is an HttpError (and optionally matches the status), false otherwise.redirect
Redirect a request. When called during request handling, SvelteKit will return a redirect response. Most common status codes:303 See Other: redirect as a GET request (often used after a form POST request)307 Temporary Redirect: redirect will keep the request method308 Permanent Redirect: redirect will keep the request method, SEO will be transferred to the new page
The HTTP status code. Must be in the range 300-308.
The location to redirect to.
This function never returns - it always throws a Redirect.
isRedirect
Checks whether an error is a redirect thrown by theredirect function.
The object to check.
Returns
true if the error is a Redirect, false otherwise.json
Create a JSONResponse object from the supplied data.
The value that will be serialized as JSON.
Options such as
status and headers that will be added to the response. Content-Type: application/json and Content-Length headers will be added automatically.A Response object with JSON content.
text
Create aResponse object from the supplied body.
The value that will be used as-is.
Options such as
status and headers that will be added to the response. A Content-Length header will be added automatically.A Response object with text content.
fail
Create anActionFailure object. Use this to return data and an appropriate status code when form submission fails.
The HTTP status code. Must be in the range 400-599.
Data associated with the failure (e.g. validation errors).
An ActionFailure object containing the status and data.
isActionFailure
Checks whether an object is an action failure returned by thefail function.
The object to check.
Returns
true if the object is an ActionFailure, false otherwise.invalid
Throw a validation error to imperatively fail form validation. Can be used in combination withissue passed to form actions to create field-specific issues.
Available since version 2.47.3
One or more validation issues. Can be issue objects or simple error message strings.
This function never returns - it always throws a ValidationError.
isValidationError
Checks whether an error is a validation error thrown by theinvalid function.
Available since version 2.47.3
The object to check.
Returns
true if the error is a ValidationError, false otherwise.normalizeUrl
Strips possible SvelteKit-internal suffixes and trailing slashes from the URL pathname. Returns the normalized URL as well as a method for adding the potential suffix back based on a new pathname (possibly including search) or URL.Available since version 2.18.0
The URL to normalize.
The normalized URL object.
Whether the URL was actually normalized (had suffixes or trailing slash).
A function to add the suffix back to a new URL.