File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed
svelte/playground/src/routes/errors/component-render Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff 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
6380The 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:
71882 . ** 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+
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