All files / src/app ErrorBoundary.tsx

85.71% Statements 6/7
100% Branches 2/2
75% Functions 3/4
100% Lines 6/6

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                                          2x     2x       1x       4x 2x   2x      
import { Component, type ErrorInfo, type ReactNode } from 'react'
 
import { reportError } from '@/lib/error-reporting'
 
import { ErrorFallback } from './ErrorFallback'
 
interface ErrorBoundaryProps {
  children: ReactNode
}
 
interface ErrorBoundaryState {
  hasError: boolean
}
 
/**
 * React only supports error boundaries via class components — there is no
 * hook or function-component equivalent for getDerivedStateFromError /
 * componentDidCatch. This is the one legitimate exception to this codebase's
 * function-components-only rule.
 */
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
  state: ErrorBoundaryState = { hasError: false }
 
  static getDerivedStateFromError(): ErrorBoundaryState {
    return { hasError: true }
  }
 
  componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    reportError(error, { componentStack: errorInfo.componentStack })
  }
 
  render() {
    if (this.state.hasError) {
      return <ErrorFallback onReload={() => window.location.assign('/')} />
    }
    return this.props.children
  }
}