From 598b644b606a861047101a584dbcb0db746b8763 Mon Sep 17 00:00:00 2001 From: MT-Mint <798521692@qq.com> Date: Mon, 13 Apr 2026 10:40:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(error):=20=E6=96=B0=E5=A2=9E=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E9=87=8D=E8=AF=95=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/core/models/api.ts | 9 +++++++++ frontend/src/core/models/hooks.ts | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/frontend/src/core/models/api.ts b/frontend/src/core/models/api.ts index 362bb0d7..d0babc18 100644 --- a/frontend/src/core/models/api.ts +++ b/frontend/src/core/models/api.ts @@ -4,6 +4,15 @@ import type { Model } from "./types"; export async function loadModels() { const res = await fetch(`${getBackendBaseURL()}/api/models`); + + if (res.status >= 500 && res.status < 600) { + throw new Error(`Server error: ${res.status}`); + } + + if (!res.ok) { + throw new Error(`HTTP error: ${res.status}`); + } + const { models } = (await res.json()) as { models: Model[] }; return models; } diff --git a/frontend/src/core/models/hooks.ts b/frontend/src/core/models/hooks.ts index 2becbbbc..f0fa7536 100644 --- a/frontend/src/core/models/hooks.ts +++ b/frontend/src/core/models/hooks.ts @@ -1,13 +1,34 @@ import { useQuery } from "@tanstack/react-query"; +import { useEffect } from "react"; +import { toast } from "sonner"; import { loadModels } from "./api"; +import type { Model } from "./types"; export function useModels({ enabled = true }: { enabled?: boolean } = {}) { - const { data, isLoading, error } = useQuery({ + const { data, isLoading, error } = useQuery({ queryKey: ["models"], queryFn: () => loadModels(), enabled, refetchOnWindowFocus: false, + retry: (failureCount, queryError) => { + if (queryError.message.startsWith("HTTP error: 4")) { + return false; + } + return failureCount < 1; + }, }); + + useEffect(() => { + // React Query v5 removed per-query onError callbacks from useQuery options. + if (error?.message.includes("Server error: 5")) { + toast.error("系统正在更新-5,请稍候……"); + } + + if (error?.message.includes("HTTP error: 4")) { + toast.error("模型接口不可用,请检查后端路由或服务状态。"); + } + }, [error]); + return { models: data ?? [], isLoading, error }; }