Skip to content

Commit 125c7ef

Browse files
docs: error boundary tutorial (#893)
* docs: error boundary tutorial * rework * tweaks * bump svelte --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 4821d7c commit 125c7ef

File tree

6 files changed

+193
-39
lines changed

6 files changed

+193
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import FlakyComponent from './FlakyComponent.svelte';
3+
</script>
4+
5+
<FlakyComponent />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
let mouse = $state({ x: 0, y: 0 });
3+
</script>
4+
5+
<svelte:window
6+
onmousemove={(e) => {
7+
mouse.x = e.clientX;
8+
mouse.y = e.clientY;
9+
}}
10+
/>
11+
12+
<p>{mouse.x}x{mouse.y}</p>
13+
14+
<button onclick={() => mouse = null}>
15+
whatever you do, don't click this button
16+
</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import FlakyComponent from './FlakyComponent.svelte';
3+
</script>
4+
5+
6+
<svelte:boundary onerror={(e) => console.error(e)}>
7+
<FlakyComponent />
8+
9+
{#snippet failed(error, reset)}
10+
<p>Oops! {error.message}</p>
11+
<button onclick={reset}>Reset</button>
12+
{/snippet}
13+
</svelte:boundary>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: <svelte:boundary>
3+
---
4+
5+
To prevent errors from leaving your app in a broken state, you can contain them inside an _error boundary_ using the `<svelte:boundary>` element.
6+
7+
In this example, `<FlakyComponent>` contains a bug — clicking the button will set `mouse` to `null`, meaning that the `{mouse.x}` and `{mouse.y}` expressions in the template will fail to render.
8+
9+
In an ideal world we'd simply fix the bug. But that's not always an option — sometimes the component belongs to someone else, and sometimes you just need to guard against the unexpected. Begin by wrapping `<FlakyComponent />` with `<svelte:boundary>`:
10+
11+
```svelte
12+
<!--- file: App.svelte --->
13+
+++<svelte:boundary>+++
14+
<FlakyComponent />
15+
+++</svelte:boundary>+++
16+
```
17+
18+
So far, nothing has changed, because the boundary doesn't specify a handler. Add a `failed` [snippet](snippets-and-render-tags) to provide some UI to show when an error occurs:
19+
20+
```svelte
21+
<!--- file: App.svelte --->
22+
<svelte:boundary>
23+
<FlakyComponent />
24+
25+
+++ {#snippet failed(error)}
26+
<p>Oops! {error.message}</p>
27+
{/snippet}+++
28+
</svelte:boundary>
29+
```
30+
31+
Now, when we click the button, the contents of the boundary are replaced with the snippet. We can attempt to reset things by making use of the second argument passed to `failed`:
32+
33+
```svelte
34+
<!--- file: App.svelte --->
35+
<svelte:boundary>
36+
<FlakyComponent />
37+
38+
{#snippet failed(error+++, reset+++)}
39+
<p>Oops! {error.message}</p>
40+
+++<button onclick={reset}>Reset</button>+++
41+
{/snippet}
42+
</svelte:boundary>
43+
```
44+
45+
We can also specify an `onerror` handler, which is called with the same arguments passed to the `failed` snippet:
46+
47+
```svelte
48+
<!--- file: App.svelte --->
49+
<svelte:boundary +++onerror={(e) => console.error(e)}+++>
50+
<FlakyComponent />
51+
52+
{#snippet failed(error, reset)}
53+
<p>Oops! {error.message}</p>
54+
<button onclick={reset}>Reset</button>
55+
{/snippet}
56+
</svelte:boundary>
57+
```
58+
59+
This is useful for sending information about the error to a reporting service, or adding UI outside the error boundary itself.

apps/svelte.dev/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"prettier-plugin-svelte": "^3.2.4",
7373
"satori": "^0.10.13",
7474
"satori-html": "^0.3.2",
75-
"svelte": "5.1.11",
75+
"svelte": "5.3.0",
7676
"svelte-check": "^4.0.0",
7777
"svelte-preprocess": "^5.1.4",
7878
"tiny-glob": "^0.2.9",

0 commit comments

Comments
 (0)