-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorBounday.tsx
95 lines (83 loc) · 2.54 KB
/
errorBounday.tsx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// components/ErrorBoundary.tsx
import React from 'react';
import * as Sentry from '@sentry/react';
interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
}
interface Props {
children: React.ReactNode;
fallback?: React.ReactNode;
onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
}
export class ProductionErrorBoundary extends React.Component<Props, ErrorBoundaryState> {
public state: ErrorBoundaryState = {
hasError: false,
error: null,
};
public static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
// Log to error monitoring service
Sentry.captureException(error, { extra: errorInfo });
// Custom error handling
this.props.onError?.(error, errorInfo);
// Log to analytics
if (window.analytics) {
window.analytics.track('Error Occurred', {
error: error.message,
componentStack: errorInfo.componentStack,
timestamp: new Date().toISOString(),
});
}
}
private handleReset = () => {
this.setState({ hasError: false, error: null });
};
public render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<div className="p-6 max-w-sm mx-auto bg-white rounded-xl shadow-md flex flex-col items-center space-y-4">
<h2 className="text-xl font-bold text-red-600">
Something went wrong
</h2>
<p className="text-gray-600 text-center">
We apologize for the inconvenience. Please try again or contact support if the problem persists.
</p>
{process.env.NODE_ENV === 'development' && (
<pre className="mt-4 p-4 bg-gray-100 rounded text-sm overflow-auto max-w-full">
{this.state.error?.stack}
</pre>
)}
<button
onClick={this.handleReset}
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
>
Try Again
</button>
</div>
);
}
return this.props.children;
}
}
// Usage:
const App = () => {
return (
<ProductionErrorBoundary
onError={(error, errorInfo) => {
// Custom error handling logic
console.error('Error caught by boundary:', error, errorInfo);
}}
fallback={
<div>Custom error UI</div>
}
>
<YourApp />
</ProductionErrorBoundary>
);
};