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-a/src/lib/FlakyCounter.svelte
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary/+assets/app-a/src/lib/StableCounter.svelte
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary/index.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,29 +2,29 @@
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 crashing your app entirely, you can guard parts of your code with so-called error boundaries, using `<svelte:boundary>`.
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>`.
6
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.
7
+
The given example contains a component that breaks after clicking its button, without a visual indicator that it crashed nor a way to recover from it.
8
8
9
-
Let's change that by wrapping `<FlakyCounter />` with `<svelte:boundary>`:
9
+
Let's change that by wrapping `<FlakyComponent />` with `<svelte:boundary>`:
10
10
11
11
```svelte
12
12
<!--- file: App.svelte --->
13
13
+++<svelte:boundary>+++
14
-
<FlakyCounter />
14
+
<FlakyComponent />
15
15
+++</svelte:boundary>+++
16
16
```
17
17
18
-
Now, when the counter errors, the boring counter continues to work — the error is contained within the boundary.
18
+
Now, when the Component errors, the error is contained within the boundary.
19
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:
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:
21
21
22
22
```svelte
23
23
<!--- file: App.svelte --->
24
24
<svelte:boundary>
25
-
<FlakyCounter />
25
+
<FlakyComponent />
26
26
+++{#snippet failed(error)}
27
-
<p>{error.message}</p>
27
+
<p>Component crashed: {error.message}</p>
28
28
{/snippet}+++
29
29
</svelte:boundary>
30
30
```
@@ -34,10 +34,10 @@ We can even reset the inner content to try again, by making use of the second ar
34
34
```svelte
35
35
<!--- file: App.svelte --->
36
36
<svelte:boundary>
37
-
<FlakyCounter />
37
+
<FlakyComponent />
38
38
{#snippet failed(error+++, reset+++)}
39
-
<p>{error.message}</p>
40
-
+++<button onclick={reset}>Try again</button>+++
39
+
<p>Component crashed: {error.message}</p>
40
+
+++<button onclick={reset}>Reset</button>+++
41
41
{/snippet}
42
42
</svelte:boundary>
43
43
```
@@ -47,10 +47,10 @@ Lastly, we "record" the error to our system by listening to the `error` event an
0 commit comments