All files / components/ErrorBoundary withErrorBoundary.tsx

87.5% Statements 7/8
92.85% Branches 13/14
75% Functions 3/4
87.5% Lines 7/8

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                            1x               8x                                                                             1x       6x 13x                       6x       6x  
import React from 'react';
import { ComponentType, ReactNode } from 'react';
import { ErrorBoundary } from './ErrorBoundary';
import { Link } from 'react-router-dom';
 
interface ErrorFallbackProps {
  error: Error | null;
  title?: string;
  message?: string;
  showHome?: boolean;
  showRefresh?: boolean;
  customActions?: ReactNode;
}
 
const ErrorFallback = ({ 
  error, 
  title = "Oops! Something went wrong",
  message,
  showHome = true,
  showRefresh = true,
  customActions
}: ErrorFallbackProps) => (
  <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">{title}</h2>
        <div className="text-gray-600 mb-6">
          {message || error?.message || 'An unexpected error occurred'}
        </div>
        <div className="space-y-4">
          {showRefresh && (
            <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>
          )}
          {showHome && (
            <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>
          )}
          {customActions}
        </div>
      </div>
    </div>
  </div>
);
 
interface WithErrorBoundaryOptions {
  title?: string;
  message?: string;
  showHome?: boolean;
  showRefresh?: boolean;
  customActions?: ReactNode;
}
 
export const withErrorBoundary = <P extends object>(
  WrappedComponent: ComponentType<P>,
  options: WithErrorBoundaryOptions = {}
) => {
  const WithErrorBoundaryComponent = (props: P) => (
    <ErrorBoundary
      fallback={
        <ErrorFallback
          error={null}
          {...options}
        />
      }
    >
      <WrappedComponent {...props} />
    </ErrorBoundary>
  );
 
  WithErrorBoundaryComponent.displayName = `WithErrorBoundary(${
    WrappedComponent.displayName || WrappedComponent.name || 'Component'
  })`;
 
  return WithErrorBoundaryComponent;
};