Errors are an inevitable fact of software development. SvelteKit handles errors differently depending on where they occur, what kind of errors they are, and the nature of the incoming request.
## Error objects
SvelteKit distinguishes between expected and unexpected errors, both of which are represented as simple `{ status: number, message: string }` objects by default.
You can add additional properties, like a `code` or a tracking `id`, as shown in the examples below. (When using TypeScript this requires you to redefine the `Error` type as described in [type safety](errors#Type-safety)).
## Expected errors
An _expected_ error is one created with the [`error`](@sveltejs-kit#error) helper imported from `@sveltejs/kit`:
```js
/// file: src/routes/blog/[slug]/+page.server.js
// @filename: ambient.d.ts
declare module '$lib/server/database' {
export function getPost(slug: string): Promise<{ title: string, content: string } | undefined>
}
// @filename: index.js
// ---cut---
import { error } from '@sveltejs/kit';
import * as db from '$lib/server/database';
/** @type {import('./$types').PageServerLoad} */
export async function load({ params }) {
const post = await db.getPost(params.slug);
if (!post) {
error(404, 'Not found');
}
return { post };
}
```
This throws an exception that SvelteKit catches, causing it to set the response status code to 404 and render an [`+error.svelte`](routing#error) component, where the `error` is an `App.Error` object with the provided `status` and `message`.
```svelte
{error.message}
```
You can add extra properties to the error object if needed:
```js
// @filename: ambient.d.ts
declare global {
namespace App {
interface Error {
message: string;
code: string;
}
}
}
export {}
// @filename: index.js
import { error } from '@sveltejs/kit';
// ---cut---
error(404, 'Not found', {
+++code: 'NOT_FOUND'+++
});
```
> [!NOTE] [In SvelteKit 1.x](migrating-to-sveltekit-2#redirect-and-error-are-no-longer-thrown-by-you) you had to `throw` the `error` yourself
## Unexpected errors
An _unexpected_ error is any other exception that occurs while handling a request. Since these can contain sensitive information, unexpected error messages and stack traces are not exposed to users.
By default, unexpected errors are printed to the console (or, in production, your server logs), while the error that is exposed to the user has a generic shape:
```json
{ "status": 500, "message": "Internal Error" }
```
Unexpected errors will go through the [`handleError`](hooks#Shared-hooks-handleError) hook, where you can add your own error handling — for example, sending errors to a reporting service, or returning a custom error object which becomes the `error` prop passed to `+error.svelte`.
You can override the HTTP status code used in the response by returning a `status` property:
```js
/// file: src/hooks.server.js
// Assuming you have this ...
class NotFound extends Error {}
/** @type {import('@sveltejs/kit').HandleServerError} */
export function handleError({ error, event, status, message }) {
// ... you can do this
if (error instanceof NotFound) {
return {
status: 404,
message: 'Not found'
};
}
return { message: 'Something went wrong' };
}
```
## Error boundaries
Errors that occur during `load` or rendering (for example inside a component's `