| 12 | } |
| 13 | |
| 14 | export class ErrorBoundary extends Component<Props, State> { |
| 15 | constructor(props: Props) { |
| 16 | super(props) |
| 17 | this.state = { hasError: false, error: null } |
| 18 | } |
| 19 | |
| 20 | static getDerivedStateFromError(error: Error): State { |
| 21 | return { hasError: true, error } |
| 22 | } |
| 23 | |
| 24 | componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { |
| 25 | console.error('ErrorBoundary caught an error:', error, errorInfo) |
| 26 | } |
| 27 | |
| 28 | handleReload = (): void => { |
| 29 | window.location.reload() |
| 30 | } |
| 31 | |
| 32 | render(): ReactNode { |
| 33 | if (this.state.hasError) { |
| 34 | if (this.props.fallback) { |
| 35 | return this.props.fallback |
| 36 | } |
| 37 | |
| 38 | return ( |
| 39 | <Result |
| 40 | status="error" |
| 41 | title="出错了" |
| 42 | subTitle={this.state.error?.message || '应用发生了未知错误'} |
| 43 | extra={ |
| 44 | <Button type="primary" onClick={this.handleReload}> |
| 45 | 重新加载 |
| 46 | </Button> |
| 47 | } |
| 48 | /> |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | return this.props.children |
| 53 | } |
| 54 | } |
nothing calls this directly
no outgoing calls
no test coverage detected