All files / components/ErrorBoundary ErrorBoundary.tsx

88.88% Statements 8/9
83.33% Branches 5/6
75% Functions 3/4
88.88% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              24x           22x       11x       46x 22x 18x     4x                                                       24x    
import React from 'react';
import { Component, ErrorInfo, ReactNode } from 'react';
import { Link } from 'react-router-dom';
 
interface Props {
  children: ReactNode;
  fallback?: ReactNode;
}
 
interface State {
  hasError: boolean;
  error: Error | null;
}
 
export class ErrorBoundary extends Component<Props, State> {
  public state: State = {
    hasError: false,
    error: null
  };
 
  public static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error };
  }
 
  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Uncaught error:', error, errorInfo);
  }
 
  public render() {
    if (this.state.hasError) {
      if (this.props.fallback) {
        return this.props.fallback;
      }
 
      return (
        <div className="min-h-screen flex items-center justify-center bg-gray-50">
          <div className="max-w-md w-full p-6 bg-white rounded-xl shadow-lg">
            <div className="text-center">
              <h2 className="text-2xl font-bold text-gray-900 mb-2">Oops! Something went wrong</h2>
              <div className="text-gray-600 mb-6">
                {this.state.error?.message || 'An unexpected error occurred'}
              </div>
              <div className="space-y-4">
                <button
                  onClick={() => window.location.reload()}
                  className="w-full bg-[#00deb6] text-white px-4 py-2 rounded-xl hover:bg-[#00c5a0] transition-colors"
                >
                  Refresh Page
                </button>
                <Link
                  to="/"
                  className="block w-full bg-white text-gray-700 border-2 border-gray-300 px-4 py-2 rounded-xl hover:border-gray-400 transition-colors"
                >
                  Go to Home
                </Link>
              </div>
            </div>
          </div>
        </div>
      );
    }
 
    return this.props.children;
  }
}