"use client"; import { Component, ReactNode, ErrorInfo } from "react"; import { log } from "@/lib/logger"; interface ErrorBoundaryState { hasError: boolean; error?: Error; } interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; onError?: (error: Error, errorInfo: ErrorInfo) => void; } /** * Error boundary component for catching and handling React errors */ export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } override componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Log to external error service in production if (process.env.NODE_ENV === "production") { // Integration point: Send to error tracking service (Sentry, LogRocket, etc.) // Example: Sentry.captureException(error, { contexts: { react: { componentStack: info.componentStack } } }); } else { log.error("ErrorBoundary caught an error", { error: error.message, stack: error.stack, componentStack: errorInfo.componentStack, }); } this.props.onError?.(error, errorInfo); } override render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (

Something went wrong

{this.state.error?.message || "An unexpected error occurred"}

); } return this.props.children; } }