From 8df3af2408b5906ad0c68581160771bf6adb5c74 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Wed, 6 Aug 2025 02:24:12 +0000 Subject: [PATCH] Add error boundary for better error handling cgen-f1de5e4d92a74cdea2ba31b82a07c3c1 --- client/components/ErrorBoundary.tsx | 81 +++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 client/components/ErrorBoundary.tsx diff --git a/client/components/ErrorBoundary.tsx b/client/components/ErrorBoundary.tsx new file mode 100644 index 00000000..8d4a6379 --- /dev/null +++ b/client/components/ErrorBoundary.tsx @@ -0,0 +1,81 @@ +import React, { Component, ErrorInfo, ReactNode } from 'react'; +import { Alert, AlertDescription } from '@/components/ui/alert'; +import { Button } from '@/components/ui/button'; +import { AlertTriangle, RefreshCw } from 'lucide-react'; + +interface Props { + children: ReactNode; +} + +interface State { + hasError: boolean; + error?: Error; + errorInfo?: ErrorInfo; +} + +class ErrorBoundary extends Component { + public state: State = { + hasError: false + }; + + public static getDerivedStateFromError(error: Error): State { + return { hasError: true, error }; + } + + public componentDidCatch(error: Error, errorInfo: ErrorInfo) { + console.error('Uncaught error:', error, errorInfo); + this.setState({ error, errorInfo }); + } + + private handleReload = () => { + window.location.reload(); + }; + + private handleReset = () => { + this.setState({ hasError: false, error: undefined, errorInfo: undefined }); + }; + + public render() { + if (this.state.hasError) { + return ( +
+
+ + + +
+

Something went wrong

+

+ An error occurred while rendering the application. This is usually temporary. +

+ {this.state.error && ( +
+ Error details +
+                        {this.state.error.message}
+                      
+
+ )} +
+
+
+ +
+ + +
+
+
+ ); + } + + return this.props.children; + } +} + +export default ErrorBoundary;