mirror of
https://github.com/basketikun/infinite-canvas.git
synced 2026-08-01 14:11:41 +08:00
fix(api): 增强API错误信息展示并修复Seedream图生图接口兼容性
This commit is contained in:
@@ -102,17 +102,53 @@ async function assertAudioBlob(blob: Blob) {
|
||||
if (payload.error?.message) throw new Error(payload.error.message);
|
||||
}
|
||||
|
||||
function readApiErrorMessage(value: unknown): string {
|
||||
if (!value) return "";
|
||||
if (typeof value === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
const inner = readApiErrorMessage(parsed) || value;
|
||||
if (inner === value && typeof parsed === "object" && Object.keys(parsed).length === 0) return "";
|
||||
return inner;
|
||||
} catch {
|
||||
if (/<[a-z][\s\S]*>/i.test(value)) return `服务返回了 HTML 错误页面(${value.slice(0, 80)}...)`;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
if (typeof value !== "object") return "";
|
||||
const payload = value as { msg?: unknown; message?: unknown; error?: unknown; detail?: unknown };
|
||||
const errorMsg =
|
||||
typeof payload.error === "string"
|
||||
? payload.error
|
||||
: (payload.error as { message?: unknown })?.message;
|
||||
return (
|
||||
readApiErrorMessage(payload.msg) ||
|
||||
readApiErrorMessage(payload.message) ||
|
||||
readApiErrorMessage(errorMsg) ||
|
||||
readApiErrorMessage(payload.detail) ||
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
function readAxiosError(error: unknown, fallback: string) {
|
||||
if (axios.isCancel(error)) return "请求已取消";
|
||||
if (axios.isAxiosError<{ error?: { message?: string }; msg?: string; code?: number }>(error)) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const responseData = error.response?.data;
|
||||
return responseData?.msg || responseData?.error?.message || statusMessage(error.response?.status, fallback);
|
||||
const apiMsg = readApiErrorMessage(responseData);
|
||||
if (apiMsg) return apiMsg;
|
||||
const statusMsg = statusMessage(error.response?.status, fallback);
|
||||
if (statusMsg) return statusMsg;
|
||||
return error.message || fallback;
|
||||
}
|
||||
return error instanceof Error ? error.message : fallback;
|
||||
if (error instanceof DOMException && error.name === "AbortError") return "请求已取消";
|
||||
return error instanceof Error ? readApiErrorMessage(error.message) || error.message : fallback;
|
||||
}
|
||||
|
||||
function statusMessage(status: number | undefined, fallback: string) {
|
||||
if (status === 401 || status === 403) return "鉴权失败,请检查 API Key、套餐权限或模型权限";
|
||||
if (status === 429) return "请求被限流或额度不足,请稍后重试";
|
||||
return status ? `${fallback}(${status})` : fallback;
|
||||
if (status === 404) return "接口地址不存在(404),请检查 Base URL 和模型选择";
|
||||
if (status === 502) return "网关错误(502),接口服务暂时不可用,请稍后重试";
|
||||
if (status === 503) return "服务繁忙(503),请稍后重试";
|
||||
return status ? `请求失败(HTTP ${status}),请检查 Base URL 和 API Key 是否正确` : fallback;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user