From 9370054911df40e1078c3134a6a81379cea2d879 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 8 Nov 2025 22:07:12 +0800 Subject: [PATCH] fix(error-handling): isolate tray menu update failures from main operations Previously, if updateTrayMenu() failed after a successful main operation (like sorting, adding, or updating providers), the entire operation would appear to fail with a misleading error message, even though the core functionality had already succeeded. This resulted in false negative feedback where: - Backend data was successfully updated - Frontend UI was successfully refreshed - Tray menu failed to update - User saw "operation failed" message (incorrect) Changes: - Wrap updateTrayMenu() calls in nested try-catch blocks - Log tray menu failures separately with descriptive messages - Ensure main operation success is reported accurately - Prevent tray menu failures from triggering main operation error handlers Files modified: - src/hooks/useDragSort.ts (drag-and-drop sorting) - src/lib/query/mutations.ts (add/delete/switch mutations) - src/hooks/useProviderActions.ts (update provider) This fixes the bug introduced in PR #179 and prevents similar issues across all provider operations. --- src/hooks/useDragSort.ts | 11 ++++++++-- src/hooks/useProviderActions.ts | 11 +++++++++- src/lib/query/mutations.ts | 36 ++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/hooks/useDragSort.ts b/src/hooks/useDragSort.ts index d2e0a5b..a2097c6 100644 --- a/src/hooks/useDragSort.ts +++ b/src/hooks/useDragSort.ts @@ -74,8 +74,15 @@ export function useDragSort(providers: Record, appId: AppId) { await queryClient.invalidateQueries({ queryKey: ["providers", appId], }); - // 更新托盘菜单以反映新的排序 - await providersApi.updateTrayMenu(); + + // 更新托盘菜单以反映新的排序(失败不影响主操作) + try { + await providersApi.updateTrayMenu(); + } catch (trayError) { + console.error("Failed to update tray menu after sort", trayError); + // 托盘菜单更新失败不影响排序成功 + } + toast.success( t("provider.sortUpdated", { defaultValue: "排序已更新", diff --git a/src/hooks/useProviderActions.ts b/src/hooks/useProviderActions.ts index 0de665b..fb683a3 100644 --- a/src/hooks/useProviderActions.ts +++ b/src/hooks/useProviderActions.ts @@ -64,7 +64,16 @@ export function useProviderActions(activeApp: AppId) { const updateProvider = useCallback( async (provider: Provider) => { await updateProviderMutation.mutateAsync(provider); - await providersApi.updateTrayMenu(); + + // 更新托盘菜单(失败不影响主操作) + try { + await providersApi.updateTrayMenu(); + } catch (trayError) { + console.error( + "Failed to update tray menu after updating provider", + trayError, + ); + } }, [updateProviderMutation], ); diff --git a/src/lib/query/mutations.ts b/src/lib/query/mutations.ts index 1e3b1a1..cf88193 100644 --- a/src/lib/query/mutations.ts +++ b/src/lib/query/mutations.ts @@ -20,7 +20,17 @@ export const useAddProviderMutation = (appId: AppId) => { }, onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ["providers", appId] }); - await providersApi.updateTrayMenu(); + + // 更新托盘菜单(失败不影响主操作) + try { + await providersApi.updateTrayMenu(); + } catch (trayError) { + console.error( + "Failed to update tray menu after adding provider", + trayError, + ); + } + toast.success( t("notifications.providerAdded", { defaultValue: "供应商已添加", @@ -76,7 +86,17 @@ export const useDeleteProviderMutation = (appId: AppId) => { }, onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ["providers", appId] }); - await providersApi.updateTrayMenu(); + + // 更新托盘菜单(失败不影响主操作) + try { + await providersApi.updateTrayMenu(); + } catch (trayError) { + console.error( + "Failed to update tray menu after deleting provider", + trayError, + ); + } + toast.success( t("notifications.deleteSuccess", { defaultValue: "供应商已删除", @@ -104,7 +124,17 @@ export const useSwitchProviderMutation = (appId: AppId) => { }, onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ["providers", appId] }); - await providersApi.updateTrayMenu(); + + // 更新托盘菜单(失败不影响主操作) + try { + await providersApi.updateTrayMenu(); + } catch (trayError) { + console.error( + "Failed to update tray menu after switching provider", + trayError, + ); + } + toast.success( t("notifications.switchSuccess", { defaultValue: "切换供应商成功",