| 17 | } |
| 18 | |
| 19 | export class ErrorBoundary extends React.Component< |
| 20 | ErrorBoundaryProps, |
| 21 | ErrorBoundaryState |
| 22 | > { |
| 23 | constructor(props: ErrorBoundaryProps) { |
| 24 | super(props) |
| 25 | this.state = { hasError: false, error: null } |
| 26 | } |
| 27 | |
| 28 | static getDerivedStateFromError(error: Error): ErrorBoundaryState { |
| 29 | return { hasError: true, error } |
| 30 | } |
| 31 | |
| 32 | componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { |
| 33 | console.error('ErrorBoundary caught an error:', error, errorInfo) |
| 34 | } |
| 35 | |
| 36 | handleReset = () => { |
| 37 | this.setState({ hasError: false, error: null }) |
| 38 | } |
| 39 | |
| 40 | render() { |
| 41 | if (this.state.hasError) { |
| 42 | return ( |
| 43 | <Card className="border-red-200 bg-red-50"> |
| 44 | <CardHeader> |
| 45 | <CardTitle className="flex items-center gap-2 text-red-800"> |
| 46 | <AlertCircle className="h-5 w-5" /> |
| 47 | {this.props.fallbackTitle || 'Something went wrong'} |
| 48 | </CardTitle> |
| 49 | </CardHeader> |
| 50 | <CardContent> |
| 51 | <p className="text-sm text-red-700 mb-4"> |
| 52 | {this.state.error?.message || 'An unexpected error occurred'} |
| 53 | </p> |
| 54 | <Button |
| 55 | onClick={this.handleReset} |
| 56 | variant="outline" |
| 57 | className="border-red-300 text-red-700 hover:bg-red-100" |
| 58 | > |
| 59 | Try Again |
| 60 | </Button> |
| 61 | </CardContent> |
| 62 | </Card> |
| 63 | ) |
| 64 | } |
| 65 | |
| 66 | return this.props.children |
| 67 | } |
| 68 | } |
nothing calls this directly
no outgoing calls
no test coverage detected