import { Component, ErrorInfo, ReactNode } from 'react'; import { getSentryInstance } from '@/lib/sentry'; interface ErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; /** Whether to report errors to Sentry. Defaults to true. */ reportToSentry?: boolean; } export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('Error caught by ErrorBoundary:', error, errorInfo); this.setState({ error, errorInfo, }); // Report to Sentry if enabled if (this.props.reportToSentry !== false) { const Sentry = getSentryInstance(); if (Sentry) { Sentry.captureException(error, { level: 'fatal', contexts: { react: { componentStack: errorInfo.componentStack ?? undefined, }, }, tags: { errorBoundary: 'true', }, }); } } } handleReset = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (

Something went wrong

An unexpected error occurred. The error has been reported.

Error details
Message:

{this.state.error?.message}

{this.state.error?.stack && (
Stack trace:
                        {this.state.error.stack}
                      
)}
); } return this.props.children; } }