import React, { Component, type ErrorInfo, type ReactNode } from 'react'; import { createLogger } from '@shared/utils/logger'; import { AlertTriangle, RefreshCw } from 'lucide-react'; const logger = createLogger('Component:ErrorBoundary'); interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo): void { logger.error('ErrorBoundary caught an error:', error, errorInfo); this.setState({ errorInfo }); } handleReload = (): void => { window.location.reload(); }; handleReset = (): void => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; // eslint-disable-next-line sonarjs/function-return-type -- Error boundaries inherently return different content based on error state render(): ReactNode { const { hasError, error, errorInfo } = this.state; const { children, fallback } = this.props; if (hasError) { if (fallback) { return fallback; } return (

Something went wrong

An unexpected error occurred in the application. You can try reloading the page or resetting the error state.

{error && (

{error.message}

{errorInfo?.componentStack && (
Component Stack
                    {errorInfo.componentStack}
                  
)}
)}
); } return children; } }