Files
template-10560/src/components/ErrorBoundary.tsx
赵忠林 217bfacadd feat(src): 新增文章、经销商申请、用户地址和礼物添加功能
- 新增文章添加页面,支持文章基本信息、设置、高级设置和图片上传
- 新增经销商申请页面,支持申请信息填写和审核状态显示
- 新增用户地址添加页面,支持地址信息填写和地址识别功能
- 新增礼物添加页面,功能与文章添加类似
- 统一使用 .tsx 文件格式
- 添加 .editorconfig、.eslintrc 和 .gitignore 文件,规范代码风格和项目结构
2025-08-20 14:56:38 +08:00

125 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { Component, ReactNode } from 'react';
import Taro from '@tarojs/taro';
import { View, Text, Button } from '@tarojs/components';
import './ErrorBoundary.scss';
interface ErrorBoundaryState {
hasError: boolean;
error?: Error;
errorInfo?: React.ErrorInfo;
}
interface ErrorBoundaryProps {
children: ReactNode;
fallback?: ReactNode;
onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
}
/**
* 全局错误边界组件
* 用于捕获React组件树中的JavaScript错误
*/
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
// 更新state下次渲染将显示错误UI
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
// 记录错误信息
this.setState({
error,
errorInfo
});
// 调用外部错误处理函数
if (this.props.onError) {
this.props.onError(error, errorInfo);
}
// 上报错误到监控系统
this.reportError(error, errorInfo);
}
// 上报错误到监控系统
private reportError = (error: Error, errorInfo: React.ErrorInfo) => {
try {
// 这里可以集成错误监控服务如Sentry、Bugsnag等
console.error('ErrorBoundary caught an error:', error, errorInfo);
// 可以发送到后端日志系统
// this.sendErrorToServer(error, errorInfo);
} catch (reportError) {
console.error('Failed to report error:', reportError);
}
};
// 重置错误状态
private handleReset = () => {
this.setState({ hasError: false, error: undefined, errorInfo: undefined });
};
// 重新加载页面
private handleReload = () => {
Taro.reLaunch({ url: '/pages/index/index' });
};
render() {
if (this.state.hasError) {
// 如果有自定义的fallback UI使用它
if (this.props.fallback) {
return this.props.fallback;
}
// 默认的错误UI
return (
<View className="error-boundary">
<View className="error-boundary__container">
<View className="error-boundary__icon">😵</View>
<Text className="error-boundary__title"></Text>
<Text className="error-boundary__message">
</Text>
{process.env.NODE_ENV === 'development' && (
<View className="error-boundary__details">
<Text className="error-boundary__error-title"></Text>
<Text className="error-boundary__error-message">
{this.state.error?.message}
</Text>
<Text className="error-boundary__error-stack">
{this.state.error?.stack}
</Text>
</View>
)}
<View className="error-boundary__actions">
<Button
className="error-boundary__button error-boundary__button--primary"
onClick={this.handleReset}
>
</Button>
<Button
className="error-boundary__button error-boundary__button--secondary"
onClick={this.handleReload}
>
</Button>
</View>
</View>
</View>
);
}
return this.props.children;
}
}
export default ErrorBoundary;