feat: differentiate fetch models error messages by failure type

Distinguish between missing API key, missing endpoint, auth failure,
unsupported provider (404/405), and timeout errors instead of showing
a generic failure toast for all cases.
This commit is contained in:
Jason
2026-04-03 23:40:49 +08:00
parent f200feebe4
commit 84998aa217
9 changed files with 119 additions and 18 deletions
+48
View File
@@ -1,4 +1,6 @@
import { invoke } from "@tauri-apps/api/core";
import type { TFunction } from "i18next";
import { toast } from "sonner";
export interface FetchedModel {
id: string;
@@ -18,3 +20,49 @@ export async function fetchModelsForConfig(
): Promise<FetchedModel[]> {
return invoke("fetch_models_for_config", { baseUrl, apiKey, isFullUrl });
}
/**
* 根据错误类型显示对应的 toast 提示
*/
export function showFetchModelsError(
err: unknown,
t: TFunction,
opts?: { hasApiKey: boolean; hasBaseUrl: boolean },
): void {
// 前端预检:缺少必填字段
if (opts && !opts.hasBaseUrl && !opts.hasApiKey) {
toast.error(t("providerForm.fetchModelsNeedConfig"));
return;
}
if (opts && !opts.hasApiKey) {
toast.error(t("providerForm.fetchModelsNeedApiKey"));
return;
}
if (opts && !opts.hasBaseUrl) {
toast.error(t("providerForm.fetchModelsNeedEndpoint"));
return;
}
// 解析后端错误字符串
const msg = String(err);
if (msg.includes("HTTP 401") || msg.includes("HTTP 403")) {
toast.error(t("providerForm.fetchModelsAuthFailed"));
return;
}
if (msg.includes("HTTP 404") || msg.includes("HTTP 405")) {
toast.error(t("providerForm.fetchModelsNotSupported"));
return;
}
if (msg.includes("timeout") || msg.includes("timed out")) {
toast.error(t("providerForm.fetchModelsTimeout"));
return;
}
if (msg.includes("Failed to parse")) {
toast.error(t("providerForm.fetchModelsNotSupported"));
return;
}
// 通用兜底
toast.error(t("providerForm.fetchModelsFailed"));
}