File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed
packages/svelte/playground Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,23 @@ Global browser error handlers that catch unhandled errors:
3535
3636** Note:** global errors will be caught using Hawk Catcher.
3737
38+ ### Error Boundaries (🟢)
39+
40+ Svelte ` <svelte:boundary> ` catches errors during:
41+
42+ - Component rendering (synchronous errors in component body)
43+ - Component initialization
44+
45+ Example usage:
46+
47+ ``` svelte
48+ <svelte:boundary onerror={handleBoundaryError} failed={fallback}>
49+ <ErrorProneComponent />
50+ </svelte:boundary>
51+ ```
52+
53+ ** Note:** error boundaries will be caught using Hawk Catcher.
54+
3855## Error Test Pages
3956
4057The playground includes test pages to demonstrate each error catching mechanism:
@@ -48,3 +65,10 @@ The playground includes test pages to demonstrate each error catching mechanism:
48652 . ** Promise Rejection** (` /errors/promise-rejection ` )
4966 - Demonstrates unhandled Promise rejection
5067 - Caught by ` window.unhandledrejection `
68+
69+ ### Error Boundaries (🟢)
70+
71+ 3 . ** Component Render Error** (` /errors/component-render ` )
72+ - Demonstrates error during component rendering
73+ - Caught by ` <svelte:boundary> `
74+
Original file line number Diff line number Diff line change 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 )));
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 ><svelte:boundary></code ></li >
4453 </ul >
4554 </li >
4655 <li >Each test demonstrates where errors are caught in the SvelteKit error handling hierarchy</li >
Original file line number Diff line number Diff line change 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 ><svelte:boundary></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 >
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments