Skip to content

Commit 477c1ce

Browse files
committed
feat(svelte-integration): add error boundary test page in svelte playground
1 parent 50c2087 commit 477c1ce

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

packages/svelte-playground/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ Global browser error handlers that catch unhandled errors:
5858

5959
**Note:** global errors will be caught using Hawk Catcher.
6060

61+
### Error Boundaries (🟡)
62+
63+
Svelte `<svelte:boundary>` catches errors during:
64+
65+
- Component rendering (synchronous errors in component body)
66+
- Component initialization
67+
68+
Example usage:
69+
70+
```svelte
71+
<svelte:boundary onerror={handleBoundaryError} failed={fallback}>
72+
<ErrorProneComponent />
73+
</svelte:boundary>
74+
```
75+
76+
**Note:** error boundaries will be caught using Hawk Catcher.
77+
6178
## Error Test Pages
6279

6380
The playground includes test pages to demonstrate each error catching mechanism:
@@ -71,3 +88,10 @@ The playground includes test pages to demonstrate each error catching mechanism:
7188
2. **Promise Rejection** (`/errors/promise-rejection`)
7289
- Demonstrates unhandled Promise rejection
7390
- Caught by `window.unhandledrejection`
91+
92+
### Error Boundaries (🟡)
93+
94+
3. **Component Render Error** (`/errors/component-render`)
95+
- Demonstrates error during component rendering
96+
- Caught by `<svelte:boundary>`
97+

packages/svelte-playground/src/routes/+page.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
href: '/errors/promise-rejection',
2121
category: 'Global Error Handlers (🔴)'
2222
},
23+
24+
// Error Boundaries
25+
{
26+
title: 'Component Rendering Error',
27+
description: 'Error thrown during component render',
28+
href: '/errors/component-render',
29+
category: 'Error Boundaries (🟡)'
30+
},
2331
];
2432
2533
const categories = Array.from(new Set(errorTests.map(t => t.category)));
@@ -41,6 +49,7 @@
4149
<li>Look for colored emoji markers:
4250
<ul>
4351
<li>🔴 = Caught by global <code>window.error</code> or <code>window.unhandledrejection</code></li>
52+
<li>🟡 = Caught by <code>&lt;svelte:boundary&gt;</code></li>
4453
</ul>
4554
</li>
4655
<li>Each test demonstrates where errors are caught in the SvelteKit error handling hierarchy</li>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script lang="ts">
2+
import ErrorComponent from './ErrorComponent.svelte';
3+
4+
let showError = $state(false);
5+
6+
function triggerError() {
7+
showError = true;
8+
}
9+
10+
function handleBoundaryError(error: Error) {
11+
console.error('🟡 [svelte:boundary] Caught rendering error:', error);
12+
}
13+
</script>
14+
15+
<div class="container">
16+
<h1>Error Boundary - Rendering Test</h1>
17+
<p>Click the button to trigger a component rendering error.</p>
18+
<p><strong>Caught by:</strong> <code>&lt;svelte:boundary&gt;</code> (🟡 yellow dot in console)</p>
19+
20+
<button onclick={triggerError} disabled={showError}>
21+
Trigger Rendering Error
22+
</button>
23+
24+
{#snippet fallback(error)}
25+
<div class="error-fallback">
26+
<h3>Error Boundary Caught Error</h3>
27+
<p>Message: {error.message}</p>
28+
</div>
29+
{/snippet}
30+
31+
<svelte:boundary onerror={handleBoundaryError} failed={fallback}>
32+
{#if showError}
33+
<ErrorComponent shouldError={true} />
34+
{/if}
35+
</svelte:boundary>
36+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
let { shouldError }: { shouldError: boolean } = $props();
3+
4+
if (shouldError) {
5+
throw new Error('Rendering error caught by svelte:boundary');
6+
}
7+
</script>
8+
9+
<div>This should not render if error is thrown</div>

0 commit comments

Comments
 (0)