Skip to content

Commit 4280b6c

Browse files
author
Rich Harris
committed
handleError
1 parent 506c8c5 commit 4280b6c

File tree

9 files changed

+79
-1
lines changed

9 files changed

+79
-1
lines changed

content/tutorial/04-advanced-sveltekit/01-hooks/04-handleerror/README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,51 @@
22
title: handleError
33
---
44

5-
> Coming soon
5+
The `handleError` hook lets you intercept unexpected errors and trigger some behaviour, like pinging a Slack channel or sending data to an error logging service.
6+
7+
As you'll recall from an [earlier exercise](error-basics), an error is _unexpected_ if it wasn't created with the `error` helper from `@sveltejs/kit`. It generally means something in your app needs fixing. The default behaviour is to log the error:
8+
9+
```js
10+
/// file: src/hooks.server.js
11+
export function handleError({ event, error }) {
12+
console.error(error.stack);
13+
}
14+
```
15+
16+
If you navigate to `/the-bad-place`, you'll see this in action — the error page is shown, and if you open the terminal (using the button to the right of the URL bar), you'll see the message from `src/routes/the-bad-place/+page.server.js`.
17+
18+
Notice that we're _not_ showing the error message to the user. That's because error messages can include sensitive information that at best will confuse your users, and at worst could benefit evildoers. Instead, the error object available to your application — represented as `$page.error` in your `+error.svelte` pages, or `%sveltekit.error%` in your `src/error.html` fallback — is just this:
19+
20+
```js
21+
/// no-file
22+
{
23+
message: 'Internal Error' // or 'Not Found' for a 404
24+
}
25+
```
26+
27+
In some situations you way want to customise this object. To do so, you can return an object from `handleError`:
28+
29+
```js
30+
/// file: src/hooks.server.js
31+
export function handleError({ event, error }) {
32+
console.error(error.stack);
33+
34+
return {
35+
message: 'everything is fine',
36+
code: 'JEREMYBEARIMY'
37+
};
38+
}
39+
```
40+
41+
You can now reference properties other than `message` in a custom error page. Create `src/routes/+error.svelte`:
42+
43+
```svelte
44+
/// file: src/routes/+error.svelte
45+
<script>
46+
import { page } from '$app/stores';
47+
</script>
48+
49+
<h1>{$page.status}</h1>
50+
<p>{$page.error.message}</p>
51+
<p>error code: {$page.error.code}</p>
52+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function handleError({ event, error }) {
2+
console.error(error.stack);
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<nav>
2+
<a href="/">home</a>
3+
<a href="/the-good-place">the good place</a>
4+
<a href="/the-bad-place">the bad place</a>
5+
</nav>
6+
7+
<slot />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>home</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function load() {
2+
throw new Error('this is the bad place!');
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>you are in the bad place</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>you are in the good place</h1>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function handleError({ event, error }) {
2+
console.error(error.stack);
3+
4+
return {
5+
message: 'everything is fine',
6+
code: 'JEREMYBEARIMY'
7+
};
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { page } from '$app/stores';
3+
</script>
4+
5+
<h1>{$page.status}</h1>
6+
<p>{$page.error.message}</p>
7+
<p>error code: {$page.error.code}</p>

0 commit comments

Comments
 (0)