mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-27 16:26:16 +08:00
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:
@@ -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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user