Skip to content

Commit 0223fb1

Browse files
committed
tweaks
1 parent 249a91c commit 0223fb1

File tree

2 files changed

+18
-15
lines changed
  • apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary

2 files changed

+18
-15
lines changed

apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary/+assets/app-b/src/lib/App.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
</script>
44

55

6-
<svelte:boundary onerror={error => console.log(error)}>
6+
<svelte:boundary onerror={(e) => console.error(e)}>
77
<FlakyComponent />
88

99
{#snippet failed(error, reset)}
10-
<p>Component crashed: {error.message}</p>
10+
<p>Oops! {error.message}</p>
1111
<button onclick={reset}>Reset</button>
1212
{/snippet}
1313
</svelte:boundary>

apps/svelte.dev/content/tutorial/02-advanced-svelte/07-special-elements/07-svelte-boundary/index.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
title: <svelte:boundary>
33
---
44

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.
66

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.
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.
88

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>`:
1010

1111
```svelte
1212
<!--- file: App.svelte --->
@@ -15,42 +15,45 @@ Let's change that by wrapping `<FlakyComponent />` with `<svelte:boundary>`:
1515
+++</svelte:boundary>+++
1616
```
1717

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:
2119

2220
```svelte
2321
<!--- file: App.svelte --->
2422
<svelte:boundary>
2523
<FlakyComponent />
26-
+++{#snippet failed(error)}
27-
<p>Component crashed: {error.message}</p>
24+
25+
+++ {#snippet failed(error)}
26+
<p>Oops! {error.message}</p>
2827
{/snippet}+++
2928
</svelte:boundary>
3029
```
3130

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`:
3332

3433
```svelte
3534
<!--- file: App.svelte --->
3635
<svelte:boundary>
3736
<FlakyComponent />
37+
3838
{#snippet failed(error+++, reset+++)}
39-
<p>Component crashed: {error.message}</p>
39+
<p>Oops! {error.message}</p>
4040
+++<button onclick={reset}>Reset</button>+++
4141
{/snippet}
4242
</svelte:boundary>
4343
```
4444

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:
4646

4747
```svelte
4848
<!--- file: App.svelte --->
49-
<svelte:boundary +++onerror={error => console.log(error)}+++>
49+
<svelte:boundary +++onerror={(e) => console.error(e)}+++>
5050
<FlakyComponent />
51+
5152
{#snippet failed(error, reset)}
52-
<p>Component crashed: {error.message}</p>
53+
<p>Oops! {error.message}</p>
5354
<button onclick={reset}>Reset</button>
5455
{/snippet}
5556
</svelte:boundary>
5657
```
58+
59+
This is useful for sending information about the error to a reporting service, or adding UI outside the error boundary itself.

0 commit comments

Comments
 (0)