2025-12-03 21:56:50 +00:00
|
|
|
import { Component, type ErrorInfo, type ReactNode } from 'react';
|
2025-12-27 00:58:49 +00:00
|
|
|
import * as Sentry from '@sentry/react';
|
|
|
|
|
import { logger, getLogContext } from '@/utils/logger';
|
2026-01-11 16:32:25 +00:00
|
|
|
import { ErrorDisplay } from '@/components/ui/ErrorDisplay';
|
2025-12-03 21:56:50 +00:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
fallback?: ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
|
hasError: boolean;
|
|
|
|
|
error?: Error;
|
|
|
|
|
errorInfo?: ErrorInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = { hasError: false };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
|
|
|
return { hasError: true, error };
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-27 00:58:49 +00:00
|
|
|
override componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
2025-12-03 21:56:50 +00:00
|
|
|
this.setState({
|
|
|
|
|
error,
|
|
|
|
|
errorInfo,
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-27 00:58:49 +00:00
|
|
|
// FIX #20: Logger l'erreur avec le logger structuré et Sentry
|
|
|
|
|
const logContext = getLogContext();
|
|
|
|
|
logger.error('[ErrorBoundary] React error caught', {
|
|
|
|
|
error: error.message,
|
|
|
|
|
stack: error.stack,
|
|
|
|
|
componentStack: errorInfo.componentStack,
|
|
|
|
|
...logContext,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Envoyer à Sentry avec contexte enrichi
|
|
|
|
|
Sentry.captureException(error, {
|
|
|
|
|
contexts: {
|
|
|
|
|
react: {
|
|
|
|
|
componentStack: errorInfo.componentStack,
|
|
|
|
|
},
|
|
|
|
|
application: logContext,
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-12-03 21:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleReset = () => {
|
|
|
|
|
this.setState({ hasError: false, error: undefined, errorInfo: undefined });
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-27 15:03:43 +00:00
|
|
|
override render() {
|
2025-12-03 21:56:50 +00:00
|
|
|
if (this.state.hasError) {
|
|
|
|
|
if (this.props.fallback) {
|
|
|
|
|
return this.props.fallback;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-11 16:32:25 +00:00
|
|
|
// Action 3.3.1.3: Use ErrorDisplay component for consistent error presentation
|
2025-12-03 21:56:50 +00:00
|
|
|
return (
|
2025-12-13 02:34:34 +00:00
|
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 dark:bg-gray-900 p-4">
|
2026-01-11 16:32:25 +00:00
|
|
|
<div className="w-full max-w-md">
|
|
|
|
|
<ErrorDisplay
|
|
|
|
|
error={this.state.error || new Error('Une erreur inattendue s\'est produite')}
|
|
|
|
|
variant="card"
|
|
|
|
|
severity="error"
|
|
|
|
|
size="lg"
|
|
|
|
|
showDetails={import.meta.env.DEV}
|
|
|
|
|
context={{
|
|
|
|
|
component: 'ErrorBoundary',
|
|
|
|
|
action: 'rendering component',
|
|
|
|
|
componentStack: this.state.errorInfo?.componentStack,
|
|
|
|
|
}}
|
|
|
|
|
onRetry={this.handleReset}
|
|
|
|
|
actions={[
|
|
|
|
|
{
|
|
|
|
|
label: 'Retour à l\'accueil',
|
|
|
|
|
onClick: () => {
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
},
|
|
|
|
|
variant: 'outline',
|
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-12-03 21:56:50 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
|
}
|
|
|
|
|
}
|