// ErrorBoundary Component // Catches JavaScript errors anywhere in the child component tree and displays a fallback UI class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false, error: null, errorInfo: null }; } static getDerivedStateFromError(error) { // Update state so the next render shows the fallback UI return { hasError: true, error }; } componentDidCatch(error, errorInfo) { // Log error details for debugging console.error('ErrorBoundary caught an error:', error, errorInfo); this.setState({ errorInfo }); // Optional: Send to error reporting service if (this.props.onError) { this.props.onError(error, errorInfo); } } handleRetry = () => { this.setState({ hasError: false, error: null, errorInfo: null }); }; handleReload = () => { window.location.reload(); }; render() { if (this.state.hasError) { // Custom fallback UI if provided if (this.props.fallback) { return this.props.fallback; } const { error, errorInfo } = this.state; const isDev = window.location.hostname === 'localhost'; return (
Si è verificato un errore imprevisto nell'applicazione
Dettagli errore (dev only)
{error.toString()}
{errorInfo?.componentStack && (
{errorInfo.componentStack}
)}
Se il problema persiste, contatta il supporto tecnico
Premi F12 per aprire la console e vedere i dettagli dell'errore
Si è verificato un errore durante il caricamento