import React, { Component, ErrorInfo, ReactNode } from 'react'; import { DomainError } from '../domain/errors'; interface ErrorBoundaryProps { children: ReactNode; fallback?: (error: Error) => ReactNode; onError?: (error: Error, errorInfo: ErrorInfo) => void; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; } export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo): void { console.error('ErrorBoundary caught error:', error, errorInfo); this.props.onError?.(error, errorInfo); } render(): ReactNode { if (this.state.hasError && this.state.error) { if (this.props.fallback) { return this.props.fallback(this.state.error); } return ; } return this.props.children; } } function DefaultErrorFallback({ error }: { error: Error }): JSX.Element { const message = error instanceof DomainError ? error.getUserMessage() : error.message; return (

出错了

{message}

技术详情
                    {error.stack}
                
); }