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:
@@ -74,8 +74,15 @@ export function useDragSort(providers: Record<string, Provider>, appId: AppId) {
|
|||||||
await queryClient.invalidateQueries({
|
await queryClient.invalidateQueries({
|
||||||
queryKey: ["providers", appId],
|
queryKey: ["providers", appId],
|
||||||
});
|
});
|
||||||
// 更新托盘菜单以反映新的排序
|
|
||||||
await providersApi.updateTrayMenu();
|
// 更新托盘菜单以反映新的排序(失败不影响主操作)
|
||||||
|
try {
|
||||||
|
await providersApi.updateTrayMenu();
|
||||||
|
} catch (trayError) {
|
||||||
|
console.error("Failed to update tray menu after sort", trayError);
|
||||||
|
// 托盘菜单更新失败不影响排序成功
|
||||||
|
}
|
||||||
|
|
||||||
toast.success(
|
toast.success(
|
||||||
t("provider.sortUpdated", {
|
t("provider.sortUpdated", {
|
||||||
defaultValue: "排序已更新",
|
defaultValue: "排序已更新",
|
||||||
|
|||||||
@@ -64,7 +64,16 @@ export function useProviderActions(activeApp: AppId) {
|
|||||||
const updateProvider = useCallback(
|
const updateProvider = useCallback(
|
||||||
async (provider: Provider) => {
|
async (provider: Provider) => {
|
||||||
await updateProviderMutation.mutateAsync(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],
|
[updateProviderMutation],
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,7 +20,17 @@ export const useAddProviderMutation = (appId: AppId) => {
|
|||||||
},
|
},
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
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(
|
toast.success(
|
||||||
t("notifications.providerAdded", {
|
t("notifications.providerAdded", {
|
||||||
defaultValue: "供应商已添加",
|
defaultValue: "供应商已添加",
|
||||||
@@ -76,7 +86,17 @@ export const useDeleteProviderMutation = (appId: AppId) => {
|
|||||||
},
|
},
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
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(
|
toast.success(
|
||||||
t("notifications.deleteSuccess", {
|
t("notifications.deleteSuccess", {
|
||||||
defaultValue: "供应商已删除",
|
defaultValue: "供应商已删除",
|
||||||
@@ -104,7 +124,17 @@ export const useSwitchProviderMutation = (appId: AppId) => {
|
|||||||
},
|
},
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
|
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(
|
toast.success(
|
||||||
t("notifications.switchSuccess", {
|
t("notifications.switchSuccess", {
|
||||||
defaultValue: "切换供应商成功",
|
defaultValue: "切换供应商成功",
|
||||||
|
|||||||
Reference in New Issue
Block a user