11/**
22 * ErrorBoundary Integration Examples
3- *
3+ *
44 * This file demonstrates how to use ErrorBoundary with braided-react
55 * to handle system startup errors gracefully.
6- *
6+ *
77 * Install: npm install react-error-boundary
88 */
99
1010import { Suspense } from "react" ;
1111import { ErrorBoundary } from "react-error-boundary" ;
1212import { defineResource } from "braided" ;
1313import { createSystemManager , createSystemHooks } from "braided-react" ;
14+ import React from "react" ;
1415
1516// ============================================================================
1617// Example 1: Basic ErrorBoundary with System Startup Errors
@@ -28,6 +29,7 @@ const basicConfig = {
2829 database : databaseResource ,
2930} ;
3031
32+ // @ts -expect-error - Type error
3133const basicManager = createSystemManager ( basicConfig ) ;
3234const { useSystem : useBasicSystem } = createSystemHooks ( basicManager ) ;
3335
@@ -89,16 +91,16 @@ function MultiApp() {
8991function CustomErrorFallback ( { error } : { error : Error } ) {
9092 // Parse the error message to extract individual failures
9193 const errorMessage = error . message ;
92-
94+
9395 // Extract individual resource errors
9496 const resourceErrors : Array < { resource : string ; message : string } > = [ ] ;
95-
97+
9698 // Parse format: "System startup failed: api: error1, cache: error2"
9799 const match = errorMessage . match ( / S y s t e m s t a r t u p f a i l e d : ( .+ ) / ) ;
98100 if ( match ) {
99101 const errorsStr = match [ 1 ] ;
100102 const parts = errorsStr . split ( ", " ) ;
101-
103+
102104 parts . forEach ( ( part ) => {
103105 const [ resource , ...messageParts ] = part . split ( ": " ) ;
104106 resourceErrors . push ( {
@@ -288,7 +290,9 @@ function CriticalFeature() {
288290
289291function OptionalFeature ( ) {
290292 return (
291- < div style = { { padding : "20px" , background : "#f0fdf4" , borderRadius : "8px" } } >
293+ < div
294+ style = { { padding : "20px" , background : "#f0fdf4" , borderRadius : "8px" } }
295+ >
292296 < h3 > ✅ Optional Features</ h3 >
293297 < p > These features work even when critical systems fail.</ p >
294298 </ div >
@@ -393,7 +397,8 @@ function ProductionErrorFallback({ error }: { error: Error }) {
393397 Something went wrong
394398 </ h1 >
395399 < p style = { { color : "#6b7280" , marginBottom : "30px" } } >
396- We're having trouble starting the application. Our team has been notified.
400+ We're having trouble starting the application. Our team has been
401+ notified.
397402 </ p >
398403 < div style = { { display : "flex" , gap : "10px" , justifyContent : "center" } } >
399404 < button
@@ -456,29 +461,28 @@ export function ProductionExample() {
456461
457462/**
458463 * Key Patterns:
459- *
464+ *
460465 * 1. Always wrap your app with ErrorBoundary + Suspense:
461466 * <ErrorBoundary FallbackComponent={ErrorUI}>
462467 * <Suspense fallback={<Loading />}>
463468 * <App />
464469 * </Suspense>
465470 * </ErrorBoundary>
466- *
471+ *
467472 * 2. System startup errors are automatically thrown by useSystem()
468473 * - Suspense catches the loading state (thrown Promise)
469474 * - ErrorBoundary catches startup failures (thrown Error)
470- *
475+ *
471476 * 3. Error messages include all failed resources:
472477 * "System startup failed: api: error1, cache: error2"
473- *
478+ *
474479 * 4. Use nested ErrorBoundaries for graceful degradation:
475480 * - Outer boundary for complete failures
476481 * - Inner boundaries for optional features
477- *
482+ *
478483 * 5. In production:
479484 * - Log errors to tracking service (Sentry, LogRocket)
480485 * - Provide user-friendly error messages
481486 * - Offer retry/reload options
482487 * - Include support contact information
483488 */
484-
0 commit comments