import React, { Component, ReactNode } from 'react'; interface Props { children: ReactNode; componentName?: string; } interface State { hasError: boolean; error: Error | null; errorInfo: React.ErrorInfo | null; } class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, errorInfo: null }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error, errorInfo: null }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); this.setState({ error, errorInfo }); } handleRetry = () => { this.setState({ hasError: false, error: null, errorInfo: null }); }; handleCopyError = () => { const { error, errorInfo } = this.state; const { componentName } = this.props; const errorText = ` Component: ${componentName || 'Unknown'} Error: ${error?.message || 'Unknown error'} Stack: ${error?.stack || 'No stack trace available'} Component Stack: ${errorInfo?.componentStack || 'No component stack available'} Timestamp: ${new Date().toISOString()} `.trim(); navigator.clipboard.writeText(errorText).then(() => { alert('Error details copied to clipboard!'); }).catch(() => { // Fallback for older browsers const textArea = document.createElement('textarea'); textArea.value = errorText; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); alert('Error details copied to clipboard!'); }); }; render() { if (this.state.hasError) { const { componentName } = this.props; const { error, errorInfo } = this.state; return (

Sorry, something went wrong

{componentName ? `An error occurred in the ${componentName} component.` : 'An unexpected error occurred.'}

Show Error Details (for developers)
Error Message:
                  {error?.message || 'Unknown error'}
                
Stack Trace:
                  {error?.stack || 'No stack trace available'}
                
{errorInfo?.componentStack && (
Component Stack:
                    {errorInfo.componentStack}
                  
)}
Timestamp: {new Date().toISOString()}
); } return this.props.children; } } export default ErrorBoundary;