/** * ============================================================================= * ERROR BOUNDARY COMPONENT * ============================================================================= * * Catches JavaScript errors in child component tree and displays fallback UI. * * INTERVIEW NOTES: * - Error boundaries only catch errors in rendering, lifecycle, and constructors * - They do NOT catch: event handlers, async code, SSR, errors in boundary itself * - Use componentDidCatch for error logging (e.g., to Sentry) * - getDerivedStateFromError updates state to show fallback UI */ import { Component, type ReactNode } from 'react'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error: Error | null; } export default class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): State { // Update state so next render shows fallback UI return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { // Log error to external service (Sentry, LogRocket, etc.) console.error('[ErrorBoundary] Caught error:', error); console.error('[ErrorBoundary] Error info:', errorInfo); // In production, send to error tracking service: // Sentry.captureException(error, { extra: errorInfo }); } handleReset = (): void => { this.setState({ hasError: false, error: null }); }; render() { if (this.state.hasError) { // Custom fallback UI if (this.props.fallback) { return this.props.fallback; } // Default fallback UI return (

Something went wrong

An unexpected error occurred. Please try again.

{import.meta.env.DEV && this.state.error && (
Error details
                  {this.state.error.message}
                  {'\n\n'}
                  {this.state.error.stack}
                
)}
); } return this.props.children; } }