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 @@ -55,6 +55,23 @@ Global browser error handlers that catch unhandled errors:
5555
5656** Note:** global errors will be caught using Hawk Catcher.
5757
58+ ### Error Boundaries (🟡)
59+
60+ Svelte ` <svelte:boundary> ` catches errors during:
61+
62+ - Component rendering (synchronous errors in component body)
63+ - Component initialization
64+
65+ Example usage:
66+
67+ ``` svelte
68+ <svelte:boundary onerror={handleBoundaryError} failed={fallback}>
69+ <ErrorProneComponent />
70+ </svelte:boundary>
71+ ```
72+
73+ ** Note:** error boundaries will be caught using Hawk Catcher.
74+
5875## Error Test Pages
5976
6077The playground includes test pages to demonstrate each error catching mechanism:
@@ -68,3 +85,10 @@ The playground includes test pages to demonstrate each error catching mechanism:
68852 . ** Promise Rejection** (` /errors/promise-rejection ` )
6986 - Demonstrates unhandled Promise rejection
7087 - Caught by ` window.unhandledrejection `
88+
89+ ### Error Boundaries (🟡)
90+
91+ 3 . ** Component Render Error** (` /errors/component-render ` )
92+ - Demonstrates error during component rendering
93+ - Caught by ` <svelte:boundary> `
94+
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