import { Component, type ErrorInfo, type ReactNode } from 'react'; import * as Sentry from '@sentry/react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { AlertTriangle, RefreshCw } from 'lucide-react'; import { logger, getLogContext } from '@/utils/logger'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error?: Error; errorInfo?: ErrorInfo; } export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error }; } override componentDidCatch(error: Error, errorInfo: ErrorInfo) { this.setState({ error, errorInfo, }); // 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, }, }); } handleReset = () => { this.setState({ hasError: false, error: undefined, errorInfo: undefined }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (
Oups ! Une erreur est survenue Une erreur inattendue s'est produite. Veuillez réessayer.
{import.meta.env.DEV && this.state.error && (

Détails de l'erreur :

                    {this.state.error.toString()}
                  
{this.state.errorInfo && (
                      {this.state.errorInfo.componentStack}
                    
)}
)}
); } return this.props.children; } }