-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
70 lines (59 loc) · 2.03 KB
/
ErrorBoundary.tsx
File metadata and controls
70 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React, { Component, ErrorInfo, ReactNode } from 'react';
import { Card } from './ui/Card';
import { Button } from './ui/Button';
import { AlertTriangle, RefreshCw } from 'lucide-react';
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
handleRetry = () => {
this.setState({ hasError: false, error: null });
window.location.reload();
};
render() {
if (this.state.hasError) {
return (
<div className="h-screen w-full flex items-center justify-center p-4">
<ErrorFallback error={this.state.error} onRetry={this.handleRetry} />
</div>
);
}
return this.props.children;
}
}
const ErrorFallback = ({ error, onRetry }: { error: Error | null, onRetry: () => void }) => {
return (
<Card className="max-w-md w-full text-center" title="Something went wrong">
<div className="flex flex-col items-center gap-4 py-4">
<div className="p-3 rounded-full bg-red-100 text-red-600 dark:bg-red-900/30 dark:text-red-400">
<AlertTriangle size={32} />
</div>
<p className="text-gray-600 dark:text-gray-300">
{error?.message || 'An unexpected error occurred while rendering this page.'}
</p>
<Button
variant="primary"
onClick={onRetry}
className="w-full flex items-center justify-center gap-2"
>
<RefreshCw size={18} />
Reload Page
</Button>
</div>
</Card>
);
};