import React, { Component, type ErrorInfo, type ReactNode } from "react"; interface ErrorBoundaryProps { children: ReactNode; suppressError?: boolean; fallback?: | (({ error, resetError }: { error: Error; resetError: () => void }) => ReactNode) | ReactNode; } interface ErrorBoundaryState { hasError: boolean; error?: Error | undefined; } class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: undefined }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } override componentDidCatch(error: Error, errorInfo: ErrorInfo) { const type = this.props.suppressError ? "warn" : "error"; console[type]("ErrorBoundary caught an error:", error, errorInfo); } resetError = () => { this.setState({ hasError: false, error: undefined }); }; private renderFallback() { if (this.props.fallback) { return typeof this.props.fallback === "function" ? ( this.props.fallback({ error: this.state.error!, resetError: this.resetError }) ) : ( {this.props.fallback} ); } return Error1; } override render() { if (this.state.hasError) { return this.renderFallback(); } if (this.props.suppressError) { try { return this.props.children; } catch (e) { return this.renderFallback(); } } return this.props.children; } } const BaseError = ({ children }: { children: ReactNode }) => (
{children}
); export default ErrorBoundary;