import { ReactNode } from 'react'; import { ErrorBoundary as ReactErrorBoundary, FallbackProps } from 'react-error-boundary'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { AlertTriangle, RefreshCw, Home } from 'lucide-react'; import { getSentryInstance } from '@/lib/sentry'; interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode | ((props: FallbackProps) => ReactNode); onError?: (error: Error, info: { componentStack?: string | null }) => void; /** Whether to report errors to Sentry (default: true) */ reportToSentry?: boolean; } /** * Default error fallback component */ function ErrorFallback({ error }: FallbackProps) { const handleReload = () => { window.location.reload(); }; const handleGoHome = () => { window.location.href = '/'; }; return (
Something went wrong We encountered an unexpected error. Don't worry, your data is safe.
Error Details {error.message} {process.env.NODE_ENV === 'development' && error.stack && (
Stack Trace
                {error.stack}
              
)}
); } /** * Error Boundary component that catches React rendering errors and displays a fallback UI. * Prevents the entire app from crashing when a component throws an error. * * Built on top of react-error-boundary for robust error handling. */ export function ErrorBoundary({ children, fallback, onError, reportToSentry = true, }: ErrorBoundaryProps) { const handleError = (error: Error, info: { componentStack?: string | null }) => { // Log error details for debugging console.error('ErrorBoundary caught an error:', error, info); // Report to Sentry if enabled if (reportToSentry) { const Sentry = getSentryInstance(); if (Sentry) { Sentry.captureException(error, { level: 'fatal', // Mark as fatal/unhandled error (skull icon in Sentry UI) contexts: { react: { componentStack: info.componentStack, }, }, tags: { errorBoundary: 'true', // Tag to easily filter these errors }, }); } } // Call optional error handler onError?.(error, info); }; // Determine fallback render const fallbackRender = typeof fallback === 'function' ? fallback : fallback ? () => fallback : ErrorFallback; return ( {children} ); }