74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import React, { Component, ErrorInfo, ReactNode } from 'react';
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error: Error | null;
|
|
}
|
|
|
|
export default 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 an error:', error, errorInfo);
|
|
}
|
|
|
|
handleReload = () => {
|
|
window.location.reload();
|
|
};
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
return (
|
|
<div className="min-h-screen w-full flex items-center justify-center bg-slate-50 p-4 font-sans">
|
|
<div className="max-w-md w-full bg-white rounded-xl shadow-lg p-8 text-center border border-slate-200">
|
|
<div className="mb-6 flex justify-center">
|
|
<div className="bg-red-100 p-3 rounded-full">
|
|
<svg
|
|
className="w-8 h-8 text-red-600"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
viewBox="0 0 24 24"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={2}
|
|
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
</div>
|
|
|
|
<h1 className="text-2xl font-bold text-slate-900 mb-2">
|
|
Etwas ist schiefgelaufen
|
|
</h1>
|
|
|
|
<p className="text-slate-600 mb-8">
|
|
{this.state.error?.message || 'Ein unerwarteter Fehler ist aufgetreten. Bitte versuche es erneut.'}
|
|
</p>
|
|
|
|
<button
|
|
onClick={this.handleReload}
|
|
className="w-full py-3 px-4 bg-indigo-600 hover:bg-indigo-700 text-white font-semibold rounded-lg transition-colors duration-200 shadow-sm"
|
|
>
|
|
Seite neu laden
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
} |