Files

55 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

import React, { Component, ErrorInfo, ReactNode } from 'react';
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
error: null,
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Uncaught error:', error, errorInfo);
}
public render() {
if (this.state.hasError) {
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-slate-950 text-slate-200 p-8">
<div className="bg-red-900/20 border border-red-500/50 rounded-xl p-8 max-w-2xl w-full text-center">
<h1 className="text-3xl font-bold text-red-500 mb-4">Something went wrong</h1>
<p className="text-slate-300 mb-6">The application encountered a critical error during rendering.</p>
<div className="bg-black/50 p-4 rounded-lg text-left overflow-auto max-h-64 font-mono text-sm border border-slate-800">
<p className="text-red-400 font-bold mb-2">Error: {this.state.error?.message}</p>
<pre className="text-slate-500 text-xs">{this.state.error?.stack}</pre>
</div>
<button
onClick={() => window.location.reload()}
className="mt-8 px-6 py-3 bg-red-600 hover:bg-red-500 text-white font-bold rounded-lg transition-colors"
>
Reload Application
</button>
</div>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;