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.
This commit is contained in:
Jason
2025-11-08 22:07:12 +08:00
parent 5b3b211c9a
commit 9370054911
3 changed files with 52 additions and 6 deletions

View File

@@ -74,8 +74,15 @@ export function useDragSort(providers: Record<string, Provider>, 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: "排序已更新",

View File

@@ -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],
);

View File

@@ -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: "切换供应商成功",