import React, { Component, ErrorInfo, ReactNode } from 'react'; import { mdiAlertCircle } from '@mdi/js'; import BaseIcon from './BaseIcon'; import { logger } from '../lib/logger'; // Define the props and state interfaces interface ErrorBoundaryProps { children: ReactNode; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; showStack: boolean; } // Class-based ErrorBoundary Component class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); // Define state variables this.state = { hasError: false, error: null, errorInfo: null, showStack: false, }; } static getDerivedStateFromError(error: Error): Partial { // Update state so the next render will show the fallback UI return { hasError: true, error: error, }; } componentDidUpdate( prevProps: Readonly, prevState: Readonly, snapshot?: any, ) { if (process.env.NODE_ENV !== 'production') { logger.debug('componentDidUpdate'); } } componentWillUnmount() { if (process.env.NODE_ENV !== 'production') { logger.debug('componentWillUnmount'); } } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Update state with error details (always needed for UI) this.setState({ errorInfo: errorInfo, }); // Only perform logging in non-production environments if (process.env.NODE_ENV !== 'production') { logger.info('Error caught in boundary:', { error, errorInfo }); } } toggleStack = () => { this.setState((prevState) => ({ showStack: !prevState.showStack, })); }; resetError = () => { this.setState({ hasError: false, error: null, errorInfo: null, showStack: false, }); }; tryAgain = () => { // Reset the error state (needed for UI recovery) this.setState({ hasError: false }); }; render() { if (this.state.hasError) { // Extract error details const { error, errorInfo, showStack } = this.state; const errorMessage = error?.message || 'An unexpected error occurred'; const stackTrace = errorInfo?.componentStack || error?.stack || 'No stack trace available'; return (

Something went wrong

We're sorry, but we encountered an unexpected error.

{errorMessage}

{showStack && (
                      {stackTrace}
                    
)}
); } return this.props.children; } } export default ErrorBoundary;