Files
LogiFlow/client/src/components/ErrorBoundary.tsx
T
2025-08-11 14:38:44 +00:00

73 lines
2.2 KiB
TypeScript

import React from 'react';
interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
}
interface ErrorBoundaryProps {
children: React.ReactNode;
fallback?: React.ComponentType<{ error: Error | null; retry: () => void }>;
}
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
retry = () => {
this.setState({ hasError: false, error: null });
};
render() {
if (this.state.hasError) {
if (this.props.fallback) {
const FallbackComponent = this.props.fallback;
return <FallbackComponent error={this.state.error} retry={this.retry} />;
}
return (
<div className="min-h-screen w-full flex items-center justify-center bg-surface">
<div className="text-center max-w-md p-6">
<div className="text-red-500 text-6xl mb-4">⚠️</div>
<h2 className="text-xl font-semibold text-gray-800 mb-2">
Une erreur s'est produite
</h2>
<p className="text-gray-600 mb-4">
L'application a rencontré un problème inattendu.
</p>
<button
onClick={this.retry}
className="px-4 py-2 bg-primary text-white rounded hover:bg-primary/90 transition-colors"
>
Réessayer
</button>
{this.state.error && (
<details className="mt-4 text-left">
<summary className="cursor-pointer text-sm text-gray-500">
Détails de l'erreur
</summary>
<pre className="mt-2 text-xs text-red-600 bg-red-50 p-2 rounded overflow-auto">
{this.state.error.toString()}
</pre>
</details>
)}
</div>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;