fix(miniprogram): 添加全局 ErrorBoundary,修复 tryRefreshToken 静默吞异常
This commit is contained in:
@@ -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;
|
||||
|
||||
38
apps/miniprogram/src/components/ErrorBoundary/index.tsx
Normal file
38
apps/miniprogram/src/components/ErrorBoundary/index.tsx
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user