import { Component, type ReactNode } from "react"; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error: Error | null; } /** * Error Boundary component to catch React rendering errors. * * Prevents the entire app from crashing when a component throws. * Displays a user-friendly error message instead of a white screen. * * Usage: * * * */ 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: React.ErrorInfo): void { // Log error for debugging console.error("ErrorBoundary caught error:", error, errorInfo); } handleRetry = (): void => { this.setState({ hasError: false, error: null }); }; render(): ReactNode { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (
Something went wrong

An unexpected error occurred while rendering the application.

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