import { Component, ReactNode } from 'react'; interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; onError?: (error: Error, errorInfo: React.ErrorInfo) => void; } interface ErrorBoundaryState { hasError: boolean; error?: Error; } /** * Error Boundary component to catch React errors and prevent full app crashes. * * Usage: * ```tsx * * * * ``` * * With custom fallback: * ```tsx * Custom error UI}> * * * ``` */ export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); this.props.onError?.(error, errorInfo); } handleReset = () => { this.setState({ hasError: false, error: undefined }); }; 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; } }