fix(miniprogram): 添加全局 ErrorBoundary,修复 tryRefreshToken 静默吞异常

This commit is contained in:
iven
2026-04-24 12:20:34 +08:00
parent f3716dbdc5
commit 74d7efec1f
3 changed files with 42 additions and 3 deletions

View File

@@ -1,8 +1,9 @@
import { PropsWithChildren } from 'react';
import ErrorBoundary from './components/ErrorBoundary';
import './app.scss';
function App({ children }: PropsWithChildren<Record<string, unknown>>) {
return children;
return <ErrorBoundary>{children}</ErrorBoundary>;
}
export default App;

View File

@@ -0,0 +1,38 @@
import React, { Component } from 'react';
import { View, Text } from '@tarojs/components';
interface Props {
children: React.ReactNode;
}
interface State {
hasError: boolean;
}
export default class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(): State {
return { hasError: true };
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
console.error('[ErrorBoundary]', error, info.componentStack);
}
render() {
if (this.state.hasError) {
return (
<View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '60vh', padding: '40px' }}>
<Text style={{ fontSize: '48px', marginBottom: '20px' }}>😵</Text>
<Text style={{ fontSize: '32px', color: '#134E4A', marginBottom: '12px' }}></Text>
<Text style={{ fontSize: '24px', color: '#94A3B8', marginBottom: '24px' }}></Text>
</View>
);
}
return this.props.children;
}
}

View File

@@ -33,8 +33,8 @@ async function tryRefreshToken(): Promise<boolean> {
Taro.setStorageSync('refresh_token', res.data.data.refresh_token);
return true;
}
} catch {
// ignore
} catch (err) {
console.error('[tryRefreshToken] token 刷新失败:', err);
}
Taro.removeStorageSync('access_token');
Taro.removeStorageSync('refresh_token');