import { Component } from 'react'; import { AlertTriangle, RefreshCw } from 'lucide-react'; // Class-based Error Boundary for catching React errors export class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { hasError: false, error: null, errorInfo: null }; } static getDerivedStateFromError(error) { return { hasError: true, error }; } componentDidCatch(error, errorInfo) { this.setState({ errorInfo }); console.error('ErrorBoundary caught an error:', error, errorInfo); } handleRetry = () => { this.setState({ hasError: false, error: null, errorInfo: null }); this.props.onRetry?.(); }; render() { if (this.state.hasError) { return (

Something went wrong

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

); } return this.props.children; } } // Functional component for displaying API/fetch errors export function ErrorState({ title = 'Failed to load', message, error, onRetry, compact = false }) { const errorMessage = message || error?.message || 'An error occurred while loading data'; if (compact) { return (
{errorMessage} {onRetry && ( )}
); } return (

{title}

{errorMessage}

{onRetry && ( )}
); } export default ErrorBoundary;