mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 20:00:42 +00:00
refactor: share provider HTTP error parsing
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
sanitizeAndNormalizeEmbedding,
|
||||
withRemoteHttpResponse,
|
||||
} from "openclaw/plugin-sdk/memory-core-host-engine-embeddings";
|
||||
import { createProviderHttpError } from "openclaw/plugin-sdk/provider-http";
|
||||
import type { GeminiEmbeddingClient, GeminiTextEmbeddingRequest } from "./embedding-provider.js";
|
||||
|
||||
export type GeminiBatchRequest = {
|
||||
@@ -179,8 +180,7 @@ async function fetchGeminiBatchStatus(params: {
|
||||
},
|
||||
onResponse: async (res) => {
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`gemini batch status failed: ${res.status} ${text}`);
|
||||
throw await createProviderHttpError(res, "gemini batch status failed");
|
||||
}
|
||||
return (await res.json()) as GeminiBatchStatus;
|
||||
},
|
||||
@@ -203,8 +203,7 @@ async function fetchGeminiFileContent(params: {
|
||||
},
|
||||
onResponse: async (res) => {
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`gemini batch file content failed: ${res.status} ${text}`);
|
||||
throw await createProviderHttpError(res, "gemini batch file content failed");
|
||||
}
|
||||
return await res.text();
|
||||
},
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
requireApiKey,
|
||||
resolveApiKeyForProvider,
|
||||
} from "openclaw/plugin-sdk/provider-auth-runtime";
|
||||
import { createProviderHttpError } from "openclaw/plugin-sdk/provider-http";
|
||||
import type { SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
|
||||
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
|
||||
|
||||
@@ -189,8 +190,7 @@ async function fetchGeminiEmbeddingPayload(params: {
|
||||
},
|
||||
onResponse: async (res) => {
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`gemini embeddings failed: ${res.status} ${text}`);
|
||||
throw await createProviderHttpError(res, "gemini embeddings failed");
|
||||
}
|
||||
return (await res.json()) as {
|
||||
embedding?: { values?: number[] };
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
createProviderHttpError,
|
||||
formatProviderHttpErrorMessage,
|
||||
} from "openclaw/plugin-sdk/provider-http";
|
||||
import {
|
||||
buildSearchCacheKey,
|
||||
buildUnsupportedSearchFilterResponse,
|
||||
@@ -81,11 +85,8 @@ async function runGeminiSearch(params: {
|
||||
},
|
||||
async (res) => {
|
||||
if (!res.ok) {
|
||||
const safeDetail = ((await res.text()) || res.statusText).replace(
|
||||
/key=[^&\s]+/giu,
|
||||
"key=***",
|
||||
);
|
||||
throw new Error(`Gemini API error (${res.status}): ${safeDetail}`);
|
||||
const error = await createProviderHttpError(res, "Gemini API error");
|
||||
throw new Error(error.message.replace(/key=[^&\s]+/giu, "key=***"));
|
||||
}
|
||||
|
||||
let data: GeminiGroundingResponse;
|
||||
@@ -99,7 +100,11 @@ async function runGeminiSearch(params: {
|
||||
if (data.error) {
|
||||
const rawMessage = data.error.message || data.error.status || "unknown";
|
||||
throw new Error(
|
||||
`Gemini API error (${data.error.code}): ${rawMessage.replace(/key=[^&\s]+/giu, "key=***")}`,
|
||||
formatProviderHttpErrorMessage({
|
||||
label: "Gemini API error",
|
||||
status: data.error.code ?? 0,
|
||||
detail: rawMessage.replace(/key=[^&\s]+/giu, "key=***"),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
type SimpleStreamOptions,
|
||||
type ThinkingLevel,
|
||||
} from "@mariozechner/pi-ai";
|
||||
import { createProviderHttpError } from "openclaw/plugin-sdk/provider-http";
|
||||
import {
|
||||
buildGuardedModelFetch,
|
||||
coerceTransportToolCallArguments,
|
||||
@@ -631,8 +632,7 @@ export function createGoogleGenerativeAiTransportStreamFn(): StreamFn {
|
||||
signal: options?.signal,
|
||||
});
|
||||
if (!response.ok) {
|
||||
const message = await response.text().catch(() => "");
|
||||
throw new Error(`Google Generative AI API error (${response.status}): ${message}`);
|
||||
throw await createProviderHttpError(response, "Google Generative AI API error");
|
||||
}
|
||||
stream.push({ type: "start", partial: output as never });
|
||||
let currentBlockIndex = -1;
|
||||
|
||||
Reference in New Issue
Block a user