fix: 优化错误处理,减少控制台噪音
- axiosConfig: 忽略 CanceledError 错误日志(组件卸载时的正常行为) - socketService: 首次连接失败使用 warn 级别,后续重试使用 debug 级别 - useEventData: 添加防御性检查,防止 pagination 为 undefined 时崩溃 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -92,9 +92,18 @@ class SocketService {
|
|||||||
// 监听连接错误
|
// 监听连接错误
|
||||||
this.socket.on('connect_error', (error) => {
|
this.socket.on('connect_error', (error) => {
|
||||||
this.reconnectAttempts++;
|
this.reconnectAttempts++;
|
||||||
logger.error('socketService', 'connect_error', error, {
|
|
||||||
attempts: this.reconnectAttempts,
|
// 首次连接失败使用 warn 级别,后续使用 debug 级别减少日志噪音
|
||||||
});
|
if (this.reconnectAttempts === 1) {
|
||||||
|
logger.warn('socketService', `Socket 连接失败,将在后台重试`, {
|
||||||
|
url: API_BASE_URL,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logger.debug('socketService', `Socket 重连尝试 #${this.reconnectAttempts}`, {
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 使用指数退避策略安排下次重连
|
// 使用指数退避策略安排下次重连
|
||||||
this.scheduleReconnection();
|
this.scheduleReconnection();
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ axios.interceptors.response.use(
|
|||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
// 忽略取消请求的错误(组件卸载时正常行为)
|
||||||
|
if (error.name === 'CanceledError' || axios.isCancel(error)) {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
const method = error.config?.method?.toUpperCase() || 'UNKNOWN';
|
const method = error.config?.method?.toUpperCase() || 'UNKNOWN';
|
||||||
const url = error.config?.url || 'UNKNOWN';
|
const url = error.config?.url || 'UNKNOWN';
|
||||||
const requestData = error.config?.data || error.config?.params || null;
|
const requestData = error.config?.data || error.config?.params || null;
|
||||||
|
|||||||
@@ -52,18 +52,21 @@ export const useEventData = (filters, pageSize = 10) => {
|
|||||||
total: response.data?.pagination?.total
|
total: response.data?.pagination?.total
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.success) {
|
if (response.success && response.data) {
|
||||||
setEvents(response.data.events);
|
const events = response.data.events || [];
|
||||||
|
const paginationData = response.data.pagination || {};
|
||||||
|
|
||||||
|
setEvents(events);
|
||||||
setPagination({
|
setPagination({
|
||||||
current: response.data.pagination.page,
|
current: paginationData.page || page,
|
||||||
pageSize: response.data.pagination.per_page,
|
pageSize: paginationData.per_page || pagination.pageSize,
|
||||||
total: response.data.pagination.total
|
total: paginationData.total || 0
|
||||||
});
|
});
|
||||||
setLastUpdateTime(new Date());
|
setLastUpdateTime(new Date());
|
||||||
|
|
||||||
logger.debug('useEventData', 'loadEvents 成功', {
|
logger.debug('useEventData', 'loadEvents 成功', {
|
||||||
count: response.data.events.length,
|
count: events.length,
|
||||||
total: response.data.pagination.total
|
total: paginationData.total || 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user