import { Component, type ErrorInfo, type ReactNode } from 'react' import { Button } from '@/components/ui/button' import { ErrorState } from '@/components/ui/error-state' export interface ErrorBoundaryFallbackProps { error: Error reset: () => void } interface ErrorBoundaryProps { children: ReactNode fallback?: (props: ErrorBoundaryFallbackProps) => ReactNode label?: string onError?: (error: Error, info: ErrorInfo) => void } interface ErrorBoundaryState { error: Error | null } export class ErrorBoundary extends Component { state: ErrorBoundaryState = { error: null } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { error } } componentDidCatch(error: Error, info: ErrorInfo) { const tag = this.props.label ? `[error-boundary:${this.props.label}]` : '[error-boundary]' console.error(tag, error, info.componentStack) this.props.onError?.(error, info) } reset = () => { this.setState({ error: null }) } render() { const { error } = this.state if (!error) { return this.props.children } if (this.props.fallback) { return this.props.fallback({ error, reset: this.reset }) } return } } function RootErrorFallback({ error, reset }: ErrorBoundaryFallbackProps) { return (
) }