/** * ErrorBoundary Component * * React error boundary for gracefully handling errors in video components. * Catches errors during rendering, in lifecycle methods, and in constructors. */ import { Component, type ErrorInfo, type ReactNode } from 'react'; import styles from './ErrorBoundary.module.css'; interface ErrorBoundaryProps { /** * Child components to wrap with error boundary */ children: ReactNode; /** * Optional custom fallback component */ fallback?: (error: Error, reset: () => void) => ReactNode; /** * Optional callback when error occurs */ onError?: (error: Error, errorInfo: ErrorInfo) => void; /** * Optional custom class name */ className?: string; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } /** * ErrorBoundary Component * * Usage: * ```tsx * * * * * // With custom fallback * ( *
*

Video error: {error.message}

* *
* )} * > * *
* ``` */ export class ErrorBoundary extends Component< ErrorBoundaryProps, ErrorBoundaryState > { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { // Update state so the next render will show the fallback UI return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Log error details console.error('[ErrorBoundary] Caught error:', error); console.error('[ErrorBoundary] Error info:', errorInfo); // Update state with error info this.setState({ errorInfo, }); // Call optional error callback if (this.props.onError) { this.props.onError(error, errorInfo); } } resetError = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; render() { if (this.state.hasError && this.state.error) { // Use custom fallback if provided if (this.props.fallback) { return this.props.fallback(this.state.error, this.resetError); } // Default error UI return (

Something went wrong

{this.state.error.message || 'An unexpected error occurred'}

{process.env.NODE_ENV === 'development' && this.state.errorInfo && (
Error Details (Development Only)
                  {this.state.errorInfo.componentStack}
                
)}
); } return this.props.children; } } /** * VideoErrorFallback Component * * Specialized error fallback for video-related errors. */ export interface VideoErrorFallbackProps { error: Error; reset: () => void; message?: string; } export function VideoErrorFallback({ error, reset, message = 'Video playback failed', }: VideoErrorFallbackProps) { return (

{message}

{error.message || 'Unable to display video'}

); } export default ErrorBoundary;