import { Component, ErrorInfo, ReactNode } from 'react'; import { AlertTriangle, RefreshCw } from 'lucide-react'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error?: Error; } /** * Error Boundary component for graceful error handling * * Catches JavaScript errors anywhere in the child component tree * and displays a fallback UI instead of crashing the whole app. */ export 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); } private handleReset = () => { this.setState({ hasError: false, error: undefined }); }; public render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (

Something went wrong

{this.state.error?.message || 'An unexpected error occurred'}

); } return this.props.children; } } /** * HOC to wrap any component with error boundary */ export function withErrorBoundary

( WrappedComponent: React.ComponentType

, fallback?: ReactNode ) { return function WithErrorBoundary(props: P) { return ( ); }; }