56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { Component, ErrorInfo, ReactNode } from 'react';
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
fallback?: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error: Error | null;
|
|
}
|
|
|
|
class ErrorBoundary extends Component<Props, State> {
|
|
constructor(props: Props) {
|
|
super(props);
|
|
this.state = { hasError: false, error: null };
|
|
}
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error };
|
|
}
|
|
|
|
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error('ErrorBoundary caught error:', error, errorInfo);
|
|
// TODO: Send to error tracking service (Sentry, LogRocket, etc.)
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
return (
|
|
this.props.fallback || (
|
|
<div className="flex items-center justify-center min-h-screen bg-gray-50">
|
|
<div className="bg-white p-8 rounded-xl shadow-lg max-w-md text-center">
|
|
<i className="fa-solid fa-triangle-exclamation text-red-500 text-5xl mb-4"></i>
|
|
<h2 className="text-2xl font-bold text-gray-800 mb-2">Something went wrong</h2>
|
|
<p className="text-gray-600 mb-4">
|
|
We're sorry, but something unexpected happened. Please refresh the page.
|
|
</p>
|
|
<button
|
|
onClick={() => window.location.reload()}
|
|
className="px-6 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors"
|
|
>
|
|
Refresh Page
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|
|
|
|
export default ErrorBoundary;
|