Skip to content

Commit 3ebe0b5

Browse files
committed
docs: error boundary tutorial
1 parent 1b686b8 commit 3ebe0b5

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import FlakyCounter from "./FlakyCounter.svelte";
3+
import StableCounter from "./StableCounter.svelte";
4+
</script>
5+
6+
<StableCounter />
7+
8+
<FlakyCounter />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let count = $state(0);
3+
</script>
4+
5+
<button onclick={() => {
6+
if (Math.random() > 0.5) {
7+
throw new Error('Oh no, counting up failed!');
8+
}
9+
count++;
10+
}}>Click this button if you're feeling adventureous</button>
11+
<p>You pressed your luck {count} times</p>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let count = $state(0);
3+
</script>
4+
5+
<button onclick={() => {
6+
count++;
7+
}}>Clicking this button is safe</button>
8+
<p>You clicked the boring button {count} times</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import FlakyCounter from "./FlakyCounter.svelte";
3+
import StableCounter from "./StableCounter.svelte";
4+
</script>
5+
6+
<StableCounter />
7+
8+
<svelte:boundary onerror={error => console.log(error)}>
9+
<FlakyCounter />
10+
11+
{#snippet failed(error, reset)}
12+
<p>{error.message}</p>
13+
<button onclick={reset}>Try again</button>
14+
{/snippet}
15+
</svelte:boundary>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: <svelte:boundary>
3+
---
4+
5+
Some areas of your app might be susceptible to errors. To prevent such errors from crashing your app entirely, you can guard parts of your code with so-called error boundaries, using `<svelte:boundary>`.
6+
7+
The given example contains a boring counter, which will never fail, and a dangerous button, which throws an error 50% of the time. Right now, if such an error is thrown, the app as a whole crashes, without a way to recover.
8+
9+
Let's change that by wrapping `<FlakyCounter />` with `<svelte:boundary>`:
10+
11+
```svelte
12+
<!--- file: App.svelte --->
13+
+++<svelte:boundary>+++
14+
<FlakyCounter />
15+
+++</svelte:boundary>+++
16+
```
17+
18+
Now, when the counter errors, the boring counter continues to work — the error is contained within the boundary.
19+
20+
While the rest of the app is still functional, it would be good to show the user that something went wrong. For that, we add the `failed` snippet to the boundary:
21+
22+
```svelte
23+
<!--- file: App.svelte --->
24+
<svelte:boundary>
25+
<FlakyCounter />
26+
+++{#snippet failed(error)}
27+
<p>{error.message}</p>
28+
{/snippet}+++
29+
</svelte:boundary>
30+
```
31+
32+
We can even reset the inner content to try again, by making use of the second argument passed to `failed`:
33+
34+
```svelte
35+
<!--- file: App.svelte --->
36+
<svelte:boundary>
37+
<FlakyCounter />
38+
{#snippet failed(error+++, reset+++)}
39+
<p>{error.message}</p>
40+
+++<button onclick={reset}>Try again</button>+++
41+
{/snippet}
42+
</svelte:boundary>
43+
```
44+
45+
Lastly, we "record" the error to our system by listening to the `error` event and `console.log`ging it:
46+
47+
```svelte
48+
<!--- file: App.svelte --->
49+
<svelte:boundary +++onerror={error => console.log(error)}+++>
50+
<FlakyCounter />
51+
{#snippet failed(error, reset)}
52+
<p>{error.message}</p>
53+
<button onclick={reset}>Try again</button>
54+
{/snippet}
55+
</svelte:boundary>
56+
```

0 commit comments

Comments
 (0)