Spaces:
Running
Running
File size: 1,922 Bytes
130ae50 |
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 |
import { Component, type ErrorInfo, type ReactNode } from "react";
import { Alert, AlertDescription, AlertTitle } from "./ui/alert";
import { Button } from "./ui/button";
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error?: Error;
}
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("ErrorBoundary caught an error:", error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div className="min-h-screen flex items-center justify-center p-8">
<div className="max-w-md w-full">
<Alert variant="destructive">
<AlertTitle>Something went wrong</AlertTitle>
<AlertDescription>
The application encountered an error. Please try refreshing the
page or contact support if the problem persists.
</AlertDescription>
</Alert>
<div className="mt-4 flex gap-2">
<Button onClick={() => window.location.reload()}>
Refresh Page
</Button>
<Button
variant="outline"
onClick={() =>
this.setState({ hasError: false, error: undefined })
}
>
Try Again
</Button>
</div>
{process.env.NODE_ENV === "development" && this.state.error && (
<div className="mt-4 p-4 bg-gray-100 rounded-md text-xs">
<pre>{this.state.error.stack}</pre>
</div>
)}
</div>
</div>
);
}
return this.props.children;
}
}
|