forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
56 lines (45 loc) 路 1.5 KB
/
Copy pathErrorBoundary.tsx
File metadata and controls
56 lines (45 loc) 路 1.5 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
'use client';
import { Component, ErrorInfo, ReactNode } from 'react';
interface Props {
children: ReactNode;
fallback?: ReactNode;
onError?: (error: Error, info: ErrorInfo) => void;
}
interface State {
error: Error | null;
}
export default class ErrorBoundary extends Component<Props, State> {
state: State = { error: null };
static getDerivedStateFromError(error: Error): State {
return { error };
}
componentDidCatch(error: Error, info: ErrorInfo) {
console.error('[ErrorBoundary]', error, info.componentStack);
this.props.onError?.(error, info);
}
reset = () => this.setState({ error: null });
render() {
const { error } = this.state;
const { children, fallback } = this.props;
if (!error) return children;
if (fallback) return fallback;
return (
<div
role="alert"
className="flex flex-col items-center gap-3 rounded-xl border border-red-200 bg-red-50 p-6 text-center dark:border-red-800 dark:bg-red-900/20"
>
<span className="text-3xl">鈿狅笍</span>
<div>
<p className="text-sm font-semibold text-red-800 dark:text-red-300">Something went wrong</p>
<p className="mt-1 text-xs text-red-600 dark:text-red-400 font-mono">{error.message}</p>
</div>
<button
onClick={this.reset}
className="rounded-lg bg-red-600 px-4 py-1.5 text-xs font-semibold text-white hover:bg-red-700 transition-colors"
>
Try again
</button>
</div>
);
}
}