import {Component, type ReactNode} from 'react'; import {Button} from '@/components/ui/button'; import {Card, CardContent, CardHeader, CardTitle} from '@/components/ui/card'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error: Error | null; } export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = {hasError: false, error: null}; } static getDerivedStateFromError(error: Error): State { return {hasError: true, error}; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { // Log error to console in development if (import.meta.env.DEV) { console.error('Error caught by boundary:', error, errorInfo); } // In production, you would send this to an error tracking service } handleReset = () => { this.setState({hasError: false, error: null}); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (
Something went wrong

An unexpected error occurred. Please try refreshing the page.

{import.meta.env.DEV && this.state.error && (

{this.state.error.message}

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