import React, { Component, ErrorInfo, ReactNode } from 'react'; import { Box, Typography, Button, Paper, Alert, AlertTitle } from '@mui/material'; import { Refresh, BugReport, Home } from '@mui/icons-material'; interface Props { children: ReactNode; } interface State { hasError: boolean; error?: Error; errorInfo?: ErrorInfo; } class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('Error caught by boundary:', error, errorInfo); this.setState({ error, errorInfo }); // Log error to monitoring service (e.g., Sentry) // logErrorToService(error, errorInfo); } handleRetry = () => { this.setState({ hasError: false, error: undefined, errorInfo: undefined }); }; handleGoHome = () => { window.location.href = '/'; }; render() { if (this.state.hasError) { return ( Something went wrong We encountered an unexpected error. Please try again. Oops! Something went wrong We're sorry, but something unexpected happened. Our team has been notified and is working to fix this issue. {process.env.NODE_ENV === 'development' && this.state.error && ( {this.state.error.toString()} )} ); } return this.props.children; } } export default ErrorBoundary;