File size: 2,493 Bytes
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from "react";
import { AlertTriangle, RotateCcw } from "lucide-react";
import { Button } from "./ui/Button";

class ErrorBoundary extends React.Component {
    constructor(props) {
        super(props);
        this.state = { hasError: false, error: null };
    }

    static getDerivedStateFromError(error) {
        return { hasError: true, error };
    }

    componentDidCatch(error, errorInfo) {
        console.error("ErrorBoundary caught an error:", error, errorInfo);
    }

    render() {
        if (this.state.hasError) {
            return (
                <div className="flex flex-col items-center justify-center min-h-[400px] p-6 text-center space-y-6 bg-card border border-border rounded-2xl shadow-sm">
                    <div className="bg-destructive/10 p-4 rounded-full text-destructive animate-pulse">
                        <AlertTriangle size={48} />
                    </div>
                    <div className="max-w-md space-y-2">
                        <h2 className="text-2xl font-bold tracking-tight">Something went wrong</h2>
                        <p className="text-muted-foreground">
                            An unexpected error occurred in this section of the application. 
                            If this persists, please try refreshing the page.
                        </p>
                    </div>
                    {this.state.error && (
                        <div className="bg-muted p-3 rounded-lg text-xs font-mono text-left max-w-full overflow-auto border border-border opacity-70">
                            {this.state.error.toString()}
                        </div>
                    )}
                    <div className="flex gap-4">
                        <Button 
                            variant="primary" 
                            onClick={() => window.location.reload()}
                            className="flex items-center gap-2"
                        >
                            <RotateCcw size={16} />
                            Reload Page
                        </Button>
                        <Button 
                            variant="ghost" 
                            onClick={() => this.setState({ hasError: false })}
                        >
                            Try Recovery
                        </Button>
                    </div>
                </div>
            );
        }

        return this.props.children;
    }
}

export default ErrorBoundary;