import { Component, type ReactNode } from "react"; import { reportError } from "../utils/errorReporting"; interface Props { children: ReactNode; } interface State { hasError: boolean; error: Error | null; } class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error }; } componentDidCatch(error: Error, info: React.ErrorInfo) { console.error("[ErrorBoundary] Uncaught error:", error, info.componentStack); reportError(error, "boundary"); } handleReset = () => { this.setState({ hasError: false, error: null }); }; handleReload = () => { window.location.reload(); }; render() { if (!this.state.hasError) { return this.props.children; } return (
⚠️

页面出错了

应用遇到了意外错误,请尝试刷新页面。
如果问题持续出现,请联系技术支持。

{this.state.error && (
错误详情
                {this.state.error.message}
              
)}
); } } export default ErrorBoundary;