All files / src/components ErrorBoundary.tsx

38.88% Statements 7/18
66.66% Branches 4/6
50% Functions 5/10
35.29% Lines 6/17

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                                                                                                                                                                                  29x     2x             889x           1x           889x 2x      
import React from "react";
 
interface ErrorBoundaryProps {
  children: React.ReactNode;
}
 
interface ErrorBoundaryState {
  error: Error | null;
}
 
/**
 * Top-level error boundary. Without one, any uncaught render exception
 * unmounts the entire React tree and leaves the user on a blank screen with
 * no way forward except a manual reload.
 */
class ErrorBoundary extends React.Component<
  ErrorBoundaryProps,
  ErrorBoundaryState
> {
  state: ErrorBoundaryState = { error: null };
 
  static getDerivedStateFromError(error: Error): ErrorBoundaryState {
    return { error };
  }
 
  componentDidCatch(error: Error, info: React.ErrorInfo) {
    console.error("Uncaught render error:", error, info.componentStack);
  }
 
  handleReload = () => {
    window.location.reload();
  };
 
  handleGoHome = () => {
    window.location.assign("/");
  };
 
  render() {
    if (!this.state.error) {
      return this.props.children;
    }
    return (
      <div className="flex min-h-screen items-center justify-center bg-white dark:bg-gray-900 px-4">
        <div className="max-w-md w-full text-center space-y-4">
          <h1 className="text-lg font-semibold text-gray-900 dark:text-white">
            Something went wrong
          </h1>
          <p className="text-sm text-gray-500 dark:text-gray-400">
            An unexpected error occurred while rendering this page. Your data
            is safe — try reloading, or head back to the overview.
          </p>
          <pre className="text-left text-xs text-red-600 dark:text-red-400 bg-gray-50 dark:bg-gray-800 rounded-lg p-3 overflow-auto max-h-32">
            {this.state.error.message}
          </pre>
          <div className="flex items-center justify-center gap-2">
            <button
              type="button"
              onClick={this.handleReload}
              className="px-4 py-2 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-sm font-medium hover:opacity-80 transition-opacity"
            >
              Reload page
            </button>
            <button
              type="button"
              onClick={this.handleGoHome}
              className="px-4 py-2 rounded-lg text-sm font-medium text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
            >
              Go to overview
            </button>
          </div>
        </div>
      </div>
    );
  }
}
 
export default ErrorBoundary;
 
interface SectionErrorBoundaryProps {
  /** A change clears the error, so moving to another step tries again. */
  resetKey: unknown;
  /** What to draw in the section's place; `reset` tries it again. */
  fallback: (error: Error, reset: () => void) => React.ReactNode;
  children: React.ReactNode;
}
 
/**
 * A boundary around one section of a page, for a page whose state is worth
 * more than the section: a wizard holding a generated proposal. The
 * app-wide boundary above replaces the whole tree and reloads, which threw
 * away a paid-for generation along with the step that failed; this one
 * draws `fallback` where the section was and leaves its parent's state be.
 */
export class SectionErrorBoundary extends React.Component<
  SectionErrorBoundaryProps,
  ErrorBoundaryState & { resetKey: unknown }
> {
  state = { error: null as Error | null, resetKey: this.props.resetKey };
 
  static getDerivedStateFromError(error: Error) {
    return { error };
  }
 
  static getDerivedStateFromProps(
    props: SectionErrorBoundaryProps,
    state: ErrorBoundaryState & { resetKey: unknown },
  ) {
    return props.resetKey !== state.resetKey
      ? { error: null, resetKey: props.resetKey }
      : null;
  }
 
  componentDidCatch(error: Error, info: React.ErrorInfo) {
    console.error("Section render error:", error, info.componentStack);
  }
 
  reset = () => this.setState({ error: null });
 
  render() {
    if (!this.state.error) return this.props.children;
    return this.props.fallback(this.state.error, this.reset);
  }
}