import React from 'react'; import { logError } from '../utils/errorLogger'; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error) { return { hasError: true, error }; } componentDidCatch(error, errorInfo) { // Extract component stack for richer context const componentInfo = { component: errorInfo?.componentStack || null, props: this.props, }; logError(error, componentInfo); } handleRetry = () => { this.setState({ hasError: false, error: null }); }; renderDefaultFallback() { return (

Something went wrong

An unexpected error occurred. You can try again.

); } render() { if (this.state.hasError) { // Allow custom fallback render function if (typeof this.props.fallback === 'function') { return this.props.fallback(this.state.error, this.handleRetry); } return this.renderDefaultFallback(); } return this.props.children; } } export default ErrorBoundary;