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
@@ -33,7 +33,11 @@ import {
copilotGetModelsForAccount,
} from "@/lib/api/copilot";
import type { CopilotModel } from "@/lib/api/copilot";
import { fetchModelsForConfig, type FetchedModel } from "@/lib/api/model-fetch";
import {
fetchModelsForConfig,
showFetchModelsError,
type FetchedModel,
} from "@/lib/api/model-fetch";
import type {
ProviderCategory,
ClaudeApiFormat,
@@ -186,7 +190,10 @@ export function ClaudeFormFields({
const handleFetchModels = useCallback(() => {
if (!baseUrl || !apiKey) {
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(null, t, {
hasApiKey: !!apiKey,
hasBaseUrl: !!baseUrl,
});
return;
}
setIsFetchingModels(true);
@@ -203,7 +210,7 @@ export function ClaudeFormFields({
})
.catch((err) => {
console.warn("[ModelFetch] Failed:", err);
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(err, t);
})
.finally(() => setIsFetchingModels(false));
}, [baseUrl, apiKey, isFullUrl, t]);
@@ -5,7 +5,11 @@ import { toast } from "sonner";
import { Download, Loader2 } from "lucide-react";
import EndpointSpeedTest from "./EndpointSpeedTest";
import { ApiKeySection, EndpointField, ModelInputWithFetch } from "./shared";
import { fetchModelsForConfig, type FetchedModel } from "@/lib/api/model-fetch";
import {
fetchModelsForConfig,
showFetchModelsError,
type FetchedModel,
} from "@/lib/api/model-fetch";
import type { ProviderCategory } from "@/types";
interface EndpointCandidate {
@@ -75,7 +79,10 @@ export function CodexFormFields({
const handleFetchModels = useCallback(() => {
if (!codexBaseUrl || !codexApiKey) {
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(null, t, {
hasApiKey: !!codexApiKey,
hasBaseUrl: !!codexBaseUrl,
});
return;
}
setIsFetchingModels(true);
@@ -92,7 +99,7 @@ export function CodexFormFields({
})
.catch((err) => {
console.warn("[ModelFetch] Failed:", err);
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(err, t);
})
.finally(() => setIsFetchingModels(false));
}, [codexBaseUrl, codexApiKey, isFullUrl, t]);
@@ -6,7 +6,11 @@ import { Button } from "@/components/ui/button";
import { toast } from "sonner";
import EndpointSpeedTest from "./EndpointSpeedTest";
import { ApiKeySection, EndpointField, ModelInputWithFetch } from "./shared";
import { fetchModelsForConfig, type FetchedModel } from "@/lib/api/model-fetch";
import {
fetchModelsForConfig,
showFetchModelsError,
type FetchedModel,
} from "@/lib/api/model-fetch";
import type { ProviderCategory } from "@/types";
interface EndpointCandidate {
@@ -74,7 +78,10 @@ export function GeminiFormFields({
const handleFetchModels = useCallback(() => {
if (!baseUrl || !apiKey) {
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(null, t, {
hasApiKey: !!apiKey,
hasBaseUrl: !!baseUrl,
});
return;
}
setIsFetchingModels(true);
@@ -91,7 +98,7 @@ export function GeminiFormFields({
})
.catch((err) => {
console.warn("[ModelFetch] Failed:", err);
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(err, t);
})
.finally(() => setIsFetchingModels(false));
}, [baseUrl, apiKey, t]);
@@ -35,7 +35,11 @@ import {
} from "@/components/ui/dropdown-menu";
import { Checkbox } from "@/components/ui/checkbox";
import { ApiKeySection } from "./shared";
import { fetchModelsForConfig, type FetchedModel } from "@/lib/api/model-fetch";
import {
fetchModelsForConfig,
showFetchModelsError,
type FetchedModel,
} from "@/lib/api/model-fetch";
import { openclawApiProtocols } from "@/config/openclawProviderPresets";
import type { ProviderCategory, OpenClawModel } from "@/types";
@@ -129,7 +133,10 @@ export function OpenClawFormFields({
// Fetch models from API
const handleFetchModels = useCallback(() => {
if (!baseUrl || !apiKey) {
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(null, t, {
hasApiKey: !!apiKey,
hasBaseUrl: !!baseUrl,
});
return;
}
setIsFetchingModels(true);
@@ -146,7 +153,7 @@ export function OpenClawFormFields({
})
.catch((err) => {
console.warn("[ModelFetch] Failed:", err);
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(err, t);
})
.finally(() => setIsFetchingModels(false));
}, [baseUrl, apiKey, t]);
@@ -28,7 +28,11 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { ApiKeySection } from "./shared";
import { fetchModelsForConfig, type FetchedModel } from "@/lib/api/model-fetch";
import {
fetchModelsForConfig,
showFetchModelsError,
type FetchedModel,
} from "@/lib/api/model-fetch";
import { opencodeNpmPackages } from "@/config/opencodeProviderPresets";
import { cn } from "@/lib/utils";
import {
@@ -247,7 +251,10 @@ export function OpenCodeFormFields({
const handleFetchModels = useCallback(() => {
if (!baseUrl || !apiKey) {
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(null, t, {
hasApiKey: !!apiKey,
hasBaseUrl: !!baseUrl,
});
return;
}
setIsFetchingModels(true);
@@ -264,7 +271,7 @@ export function OpenCodeFormFields({
})
.catch((err) => {
console.warn("[ModelFetch] Failed:", err);
toast.error(t("providerForm.fetchModelsFailed"));
showFetchModelsError(err, t);
})
.finally(() => setIsFetchingModels(false));
}, [baseUrl, apiKey, t]);