Spaces:
Running
Running
Create frontend/src/components/ErrorBoundary.jsx
Browse files
frontend/src/components/ErrorBoundary.jsx
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from "react";
|
| 2 |
+
|
| 3 |
+
class ErrorBoundary extends React.Component {
|
| 4 |
+
constructor(props) {
|
| 5 |
+
super(props);
|
| 6 |
+
this.state = { hasError: false, error: null };
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
static getDerivedStateFromError(error) {
|
| 10 |
+
return { hasError: true, error };
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
componentDidCatch(error, errorInfo) {
|
| 14 |
+
console.error("Error caught by boundary:", error, errorInfo);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
render() {
|
| 18 |
+
if (this.state.hasError) {
|
| 19 |
+
return (
|
| 20 |
+
<div className="min-h-screen bg-[#FAFAFA] flex items-center justify-center p-8">
|
| 21 |
+
<div className="max-w-md w-full bg-white rounded-2xl border border-red-200 p-8 shadow-lg">
|
| 22 |
+
<div className="text-center">
|
| 23 |
+
<div className="h-16 w-16 mx-auto rounded-full bg-red-100 flex items-center justify-center mb-4">
|
| 24 |
+
<svg
|
| 25 |
+
className="h-8 w-8 text-red-600"
|
| 26 |
+
fill="none"
|
| 27 |
+
viewBox="0 0 24 24"
|
| 28 |
+
stroke="currentColor"
|
| 29 |
+
>
|
| 30 |
+
<path
|
| 31 |
+
strokeLinecap="round"
|
| 32 |
+
strokeLinejoin="round"
|
| 33 |
+
strokeWidth={2}
|
| 34 |
+
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
| 35 |
+
/>
|
| 36 |
+
</svg>
|
| 37 |
+
</div>
|
| 38 |
+
<h2 className="text-xl font-bold text-slate-900 mb-2">
|
| 39 |
+
Something went wrong
|
| 40 |
+
</h2>
|
| 41 |
+
<p className="text-sm text-slate-600 mb-6">
|
| 42 |
+
The application encountered an error. Please refresh the page or contact support if the problem persists.
|
| 43 |
+
</p>
|
| 44 |
+
<button
|
| 45 |
+
onClick={() => window.location.reload()}
|
| 46 |
+
className="px-6 py-2 bg-indigo-600 text-white rounded-lg font-semibold hover:bg-indigo-700 transition-colors"
|
| 47 |
+
>
|
| 48 |
+
Refresh Page
|
| 49 |
+
</button>
|
| 50 |
+
{process.env.NODE_ENV === "development" && this.state.error && (
|
| 51 |
+
<details className="mt-6 text-left">
|
| 52 |
+
<summary className="text-sm text-slate-500 cursor-pointer mb-2">
|
| 53 |
+
Error Details (Development Only)
|
| 54 |
+
</summary>
|
| 55 |
+
<pre className="text-xs bg-slate-100 p-4 rounded-lg overflow-auto max-h-64">
|
| 56 |
+
{this.state.error.toString()}
|
| 57 |
+
{this.state.error.stack}
|
| 58 |
+
</pre>
|
| 59 |
+
</details>
|
| 60 |
+
)}
|
| 61 |
+
</div>
|
| 62 |
+
</div>
|
| 63 |
+
</div>
|
| 64 |
+
);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
return this.props.children;
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
export default ErrorBoundary;
|
| 72 |
+
|