import { Component } from 'react'; import type { ErrorInfo, ReactNode } from 'react'; interface Props { children: ReactNode; fallback?: ReactNode; onError?: (error: Error, errorInfo: ErrorInfo) => void; } interface State { hasError: boolean; error: Error | null; } export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); // Call optional error handler if (this.props.onError) { this.props.onError(error, errorInfo); } // Log to error tracking service (e.g., Sentry) if (import.meta.env.PROD) { // TODO: Send to error tracking service console.error('Production error:', { error: error.toString(), componentStack: errorInfo.componentStack, }); } } handleReset = () => { this.setState({ hasError: false, error: null, }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (

Something went wrong

An unexpected error occurred. Please try refreshing the page.

{import.meta.env.DEV && this.state.error && (

{this.state.error.toString()}

)}
); } return this.props.children; } }