Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript
1.7 KB
Raw Blame
import React from "react";
import i18n from "@/i18n/i18n";

// Define the types for props and state
interface ErrorBoundaryProps {
    children: React.ReactNode;
}

interface ErrorBoundaryState {
    hasError: boolean;
    error: Error | null;
    errorInfo: React.ErrorInfo | null;
}

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

    static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> {
        // Update state so the next render shows the fallback UI.
        return {hasError: true};
    }

    componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
        // You can also log the error to an error reporting service
        console.error('Error caught by ErrorBoundary', error, errorInfo);
        // Store the error and errorInfo in state
        this.setState({error, errorInfo});
    }

    render() {
        if (this.state.hasError) {
            // Render a custom fallback UI with error details
            return (
                <div>
                    <h1>{i18n.t('somethingWentWrong')}</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;
// // Usage
// <ErrorBoundary>
//     <ThreeJSCanvas3d />
// </ErrorBoundary>