Skip to content

Commit 249a91c

Browse files
committed
rework
1 parent 3ebe0b5 commit 249a91c

File tree

6 files changed

+36
-44
lines changed

6 files changed

+36
-44
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<script>
2-
import FlakyCounter from "./FlakyCounter.svelte";
3-
import StableCounter from "./StableCounter.svelte";
2+
import FlakyComponent from './FlakyComponent.svelte';
43
</script>
54

6-
<StableCounter />
7-
8-
<FlakyCounter />
5+
<FlakyComponent />
Lines changed: 16 additions & 0 deletions
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>

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<script>
2-
import FlakyCounter from "./FlakyCounter.svelte";
3-
import StableCounter from "./StableCounter.svelte";
2+
import FlakyComponent from './FlakyComponent.svelte';
43
</script>
54

6-
<StableCounter />
75

86
<svelte:boundary onerror={error => console.log(error)}>
9-
<FlakyCounter />
7+
<FlakyComponent />
108

119
{#snippet failed(error, reset)}
12-
<p>{error.message}</p>
13-
<button onclick={reset}>Try again</button>
10+
<p>Component crashed: {error.message}</p>
11+
<button onclick={reset}>Reset</button>
1412
{/snippet}
1513
</svelte:boundary>

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

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

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

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

9-
Let's change that by wrapping `<FlakyCounter />` with `<svelte:boundary>`:
9+
Let's change that by wrapping `<FlakyComponent />` with `<svelte:boundary>`:
1010

1111
```svelte
1212
<!--- file: App.svelte --->
1313
+++<svelte:boundary>+++
14-
<FlakyCounter />
14+
<FlakyComponent />
1515
+++</svelte:boundary>+++
1616
```
1717

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

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

2222
```svelte
2323
<!--- file: App.svelte --->
2424
<svelte:boundary>
25-
<FlakyCounter />
25+
<FlakyComponent />
2626
+++{#snippet failed(error)}
27-
<p>{error.message}</p>
27+
<p>Component crashed: {error.message}</p>
2828
{/snippet}+++
2929
</svelte:boundary>
3030
```
@@ -34,10 +34,10 @@ We can even reset the inner content to try again, by making use of the second ar
3434
```svelte
3535
<!--- file: App.svelte --->
3636
<svelte:boundary>
37-
<FlakyCounter />
37+
<FlakyComponent />
3838
{#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>+++
4141
{/snippet}
4242
</svelte:boundary>
4343
```
@@ -47,10 +47,10 @@ Lastly, we "record" the error to our system by listening to the `error` event an
4747
```svelte
4848
<!--- file: App.svelte --->
4949
<svelte:boundary +++onerror={error => console.log(error)}+++>
50-
<FlakyCounter />
50+
<FlakyComponent />
5151
{#snippet failed(error, reset)}
52-
<p>{error.message}</p>
53-
<button onclick={reset}>Try again</button>
52+
<p>Component crashed: {error.message}</p>
53+
<button onclick={reset}>Reset</button>
5454
{/snippet}
5555
</svelte:boundary>
5656
```

0 commit comments

Comments
 (0)