You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary/+assets/app-b/src/lib/App.svelte
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary/index.md
+16-13Lines changed: 16 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
title: <svelte:boundary>
3
3
---
4
4
5
-
Some areas of your app might be susceptible to errors. To prevent such errors from putting your app in a broken state, you can guard parts of your code with so-called error boundaries, using `<svelte:boundary>`.
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
6
7
-
The given examplecontains a component that breaks after clicking its button, without a visual indicator that it crashed nor a way to recover from it.
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
8
9
-
Let's change that by wrapping `<FlakyComponent />` with `<svelte:boundary>`:
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
10
11
11
```svelte
12
12
<!--- file: App.svelte --->
@@ -15,42 +15,45 @@ Let's change that by wrapping `<FlakyComponent />` with `<svelte:boundary>`:
15
15
+++</svelte:boundary>+++
16
16
```
17
17
18
-
Now, when the Component errors, the error is contained within the boundary.
19
-
20
-
While the rest of the app is now safe from any unwanted side effects of the error, it would be good to show the user that something went wrong. For that, we add the `failed` snippet to the boundary:
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:
21
19
22
20
```svelte
23
21
<!--- file: App.svelte --->
24
22
<svelte:boundary>
25
23
<FlakyComponent />
26
-
+++{#snippet failed(error)}
27
-
<p>Component crashed: {error.message}</p>
24
+
25
+
+++ {#snippet failed(error)}
26
+
<p>Oops! {error.message}</p>
28
27
{/snippet}+++
29
28
</svelte:boundary>
30
29
```
31
30
32
-
We can even reset the inner content to try again, by making use of the second argument passed to `failed`:
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`:
33
32
34
33
```svelte
35
34
<!--- file: App.svelte --->
36
35
<svelte:boundary>
37
36
<FlakyComponent />
37
+
38
38
{#snippet failed(error+++, reset+++)}
39
-
<p>Component crashed: {error.message}</p>
39
+
<p>Oops! {error.message}</p>
40
40
+++<button onclick={reset}>Reset</button>+++
41
41
{/snippet}
42
42
</svelte:boundary>
43
43
```
44
44
45
-
Lastly, we "record" the error to our system by listening to the `error` event and `console.log`ging it:
45
+
We can also specify an `onerror` handler, which is called with the same arguments passed to the `failed` snippet:
0 commit comments