import React, { Component } from 'react'; import { Button, Result, Space, Collapse, Typography } from 'antd'; import { WarningOutlined, ReloadOutlined, HomeOutlined, BugOutlined, } from '@ant-design/icons'; const { Text, Paragraph } = Typography; const { Panel } = Collapse; class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, errorInfo) { this.setState({ error, errorInfo, }); console.error('错误边界捕获到错误:', error, errorInfo); if (this.props.onError) { this.props.onError(error, errorInfo); } } handleReload = () => { window.location.reload(); }; handleGoHome = () => { window.location.href = '/'; }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (
} extra={ } > {this.state.error && (
查看错误详情(开发环境) ), children: ( <> {this.state.error.toString()} {this.state.errorInfo?.componentStack && ( {this.state.errorInfo.componentStack} )} ), }, ]} />
)}
); } return this.props.children; } } ErrorBoundary.defaultProps = { fallback: null, fullPage: false, title: null, subTitle: null, onError: null, }; export default ErrorBoundary;