Skip to main content
Errors are inevitable in software development. SvelteKit handles errors differently depending on where they occur and what kind they are.

Error objects

SvelteKit distinguishes between expected and unexpected errors, both represented as simple { message: string } objects by default.
You can add additional properties like code or tracking id by customizing the App.Error type.

Expected errors

An expected error is created with the error helper from @sveltejs/kit:
src/routes/blog/[slug]/+page.server.js
This causes SvelteKit to:
1

Set the response status

Sets the HTTP status code to 404
2

Render error page

Renders the nearest +error.svelte component
3

Pass error object

Makes the error object available as page.error

Displaying errors

src/routes/+error.svelte

Adding custom properties

Shorthand syntax

For convenience, pass a string as the second argument:

Unexpected errors

An unexpected error is any other exception that occurs while handling a request. These can contain sensitive information, so messages and stack traces are not exposed to users.
By default, unexpected errors are printed to the console but show a generic message to users:

handleError hook

Unexpected errors go through the handleError hook, where you can:
src/hooks.server.js
src/hooks.server.js
src/hooks.client.js
Make sure that handleError never throws an error itself.

Error responses

How SvelteKit responds to errors depends on where they occur:

In handle or +server.js

SvelteKit responds with either:
  • Fallback error page (if Accept header expects HTML)
  • JSON representation (if Accept header is application/json)

Custom fallback page

Create src/error.html to customize the fallback error page:
src/error.html
SvelteKit replaces these placeholders:
  • %sveltekit.status% → HTTP status code
  • %sveltekit.error.message% → Error message

In load functions

If an error occurs inside a load function, SvelteKit renders the nearest +error.svelte component.
  • Errors in +page.server.js or +page.js render the nearest sibling +error.svelte
  • Errors in +layout.server.js or +layout.js render the nearest parent +error.svelte (not the sibling)
  • Errors in root +layout.js or +layout.server.js use the fallback error page

Type safety

Customize the shape of errors with TypeScript:
src/app.d.ts
The App.Error interface always includes a message: string property.

Error helper reference

From @sveltejs/kit:
function
Throws an HTTP error with a status code and message
function
Checks if an error was thrown by the error helper

Common patterns

Learn more

Follow the interactive tutorial on errors and redirects