File size: 1,122 Bytes
619120c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';

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

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

    componentDidCatch(error, errorInfo) {
        console.error("Uncaught error:", error, errorInfo);
        this.setState({ errorInfo });
    }

    render() {
        if (this.state.hasError) {
            return (
                <div style={{ padding: '20px', color: 'red', background: '#ffebee', fontFamily: 'monospace' }}>

                    <h1>Something went wrong.</h1>

                    <details style={{ whiteSpace: 'pre-wrap' }}>

                        {this.state.error && this.state.error.toString()}

                        <br />

                        {this.state.errorInfo && this.state.errorInfo.componentStack}

                    </details>

                </div>
            );
        }

        return this.props.children;
    }
}

export default ErrorBoundary;