|
| 1 | +import React, { Component } from "react"; |
| 2 | +import { Button, Modal } from "antd"; |
| 3 | + |
| 4 | +interface ErrorContextType { |
| 5 | + hasError: boolean; |
| 6 | + error: Error | null; |
| 7 | + errorInfo: { componentStack: string } | null; |
| 8 | +} |
| 9 | + |
| 10 | +const ErrorContext = React.createContext<ErrorContextType>({ |
| 11 | + hasError: false, |
| 12 | + error: null, |
| 13 | + errorInfo: null, |
| 14 | +}); |
| 15 | + |
| 16 | +interface ErrorBoundaryState { |
| 17 | + hasError: boolean; |
| 18 | + error: Error | null; |
| 19 | + errorInfo: { componentStack: string } | null; |
| 20 | + errorTimestamp: string | null; |
| 21 | +} |
| 22 | + |
| 23 | +interface ErrorBoundaryProps { |
| 24 | + children?: React.ReactNode; |
| 25 | + onReset?: () => void; |
| 26 | + showDetails?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +export default class ErrorBoundary extends Component< |
| 30 | + ErrorBoundaryProps, |
| 31 | + ErrorBoundaryState |
| 32 | +> { |
| 33 | + constructor(props: ErrorBoundaryProps) { |
| 34 | + super(props); |
| 35 | + this.state = { |
| 36 | + hasError: false, |
| 37 | + error: null, |
| 38 | + errorInfo: null, |
| 39 | + errorTimestamp: null, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + static getDerivedStateFromError(error: any) { |
| 44 | + // 更新 state 使下一次渲染能够显示降级 UI |
| 45 | + return { |
| 46 | + hasError: true, |
| 47 | + error: error, |
| 48 | + errorTimestamp: new Date().toISOString(), |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { |
| 53 | + // 错误统计 |
| 54 | + this.setState({ |
| 55 | + error, |
| 56 | + errorInfo, |
| 57 | + hasError: true, |
| 58 | + }); |
| 59 | + |
| 60 | + // 在实际应用中,这里可以集成错误报告服务 |
| 61 | + this.logErrorToService(error, errorInfo); |
| 62 | + |
| 63 | + // 开发环境下在控制台显示详细错误 |
| 64 | + if (process.env.NODE_ENV === "development") { |
| 65 | + console.error("ErrorBoundary 捕获到错误:", error); |
| 66 | + console.error("错误详情:", errorInfo); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + logErrorToService = (error: Error, errorInfo: React.ErrorInfo) => { |
| 71 | + // 这里可以集成 Sentry、LogRocket 等错误监控服务 |
| 72 | + const errorData = { |
| 73 | + error: error.toString(), |
| 74 | + errorInfo: errorInfo.componentStack, |
| 75 | + timestamp: this.state.errorTimestamp, |
| 76 | + url: window.location.href, |
| 77 | + userAgent: navigator.userAgent, |
| 78 | + }; |
| 79 | + |
| 80 | + // 模拟发送错误日志 |
| 81 | + console.log("发送错误日志到监控服务:", errorData); |
| 82 | + |
| 83 | + // 实际使用时取消注释并配置您的错误监控服务 |
| 84 | + /* |
| 85 | + if (window.Sentry) { |
| 86 | + window.Sentry.captureException(error, { extra: errorInfo }); |
| 87 | + } |
| 88 | + */ |
| 89 | + }; |
| 90 | + |
| 91 | + handleReset = () => { |
| 92 | + this.setState({ |
| 93 | + hasError: false, |
| 94 | + error: null, |
| 95 | + errorInfo: null, |
| 96 | + errorTimestamp: null, |
| 97 | + }); |
| 98 | + |
| 99 | + // 可选:重新加载页面或执行其他恢复操作 |
| 100 | + if (this.props.onReset) { |
| 101 | + this.props.onReset(); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + handleReload = () => { |
| 106 | + window.location.reload(); |
| 107 | + }; |
| 108 | + |
| 109 | + handleGoHome = () => { |
| 110 | + window.location.href = "/"; |
| 111 | + }; |
| 112 | + |
| 113 | + renderErrorDetails = () => { |
| 114 | + const { error, errorInfo } = this.state; |
| 115 | + |
| 116 | + if (!this.props.showDetails) return null; |
| 117 | + |
| 118 | + return ( |
| 119 | + <div className="bg-gray-100 p-4 mt-4 text-left rounded"> |
| 120 | + <div className="mt-2"> |
| 121 | + <strong>错误信息:</strong> |
| 122 | + <pre className="bg-gray-600 px-4 py-2 rounded text-white overflow-auto"> |
| 123 | + {error?.toString()} |
| 124 | + </pre> |
| 125 | + </div> |
| 126 | + {errorInfo && ( |
| 127 | + <div className="mt-2"> |
| 128 | + <strong>组件堆栈:</strong> |
| 129 | + <pre className="bg-gray-600 px-4 py-2 rounded text-white overflow-auto"> |
| 130 | + {errorInfo.componentStack} |
| 131 | + </pre> |
| 132 | + </div> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + ); |
| 136 | + }; |
| 137 | + |
| 138 | + render() { |
| 139 | + if (this.state.hasError) { |
| 140 | + return ( |
| 141 | + <Modal visible width={1000} footer={null} closable={false}> |
| 142 | + <div className="text-center p-6"> |
| 143 | + <div className="text-3xl">⚠️</div> |
| 144 | + <h1 className="text-xl p-2">出了点问题</h1> |
| 145 | + <p className="text-sm text-gray-400">应用程序遇到了意外错误。</p> |
| 146 | + |
| 147 | + <div className="flex justify-center gap-4 my-4"> |
| 148 | + <Button onClick={this.handleReload}>刷新页面</Button> |
| 149 | + <Button type="primary" onClick={this.handleGoHome}> |
| 150 | + 返回首页 |
| 151 | + </Button> |
| 152 | + </div> |
| 153 | + |
| 154 | + {this.renderErrorDetails()} |
| 155 | + |
| 156 | + <div className="mt-4 border-t border-gray-100 pt-4 text-center"> |
| 157 | + <p className="text-sm text-gray-500"> |
| 158 | + 如果问题持续存在,请联系技术支持 |
| 159 | + </p> |
| 160 | + <small className="text-xs text-gray-400"> |
| 161 | + 错误 ID: {this.state.errorTimestamp} |
| 162 | + </small> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + </Modal> |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + return ( |
| 170 | + <ErrorContext.Provider |
| 171 | + value={{ |
| 172 | + hasError: this.state.hasError, |
| 173 | + error: this.state.error, |
| 174 | + errorInfo: this.state.errorInfo, |
| 175 | + }} |
| 176 | + > |
| 177 | + {this.props.children} |
| 178 | + </ErrorContext.Provider> |
| 179 | + ); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +export function withErrorBoundary( |
| 184 | + Component: React.ComponentType |
| 185 | +): React.ComponentType { |
| 186 | + return (props) => ( |
| 187 | + <ErrorBoundary showDetails={process.env.NODE_ENV === "development"}> |
| 188 | + <Component {...props} /> |
| 189 | + </ErrorBoundary> |
| 190 | + ); |
| 191 | +} |
0 commit comments