diff --git a/src/renderer/src/api/agent.ts b/src/renderer/src/api/agent.ts index 29606f1a2f..d3efc2aeeb 100644 --- a/src/renderer/src/api/agent.ts +++ b/src/renderer/src/api/agent.ts @@ -1,5 +1,5 @@ import { loggerService } from '@logger' -import { formatAgentServerError } from '@renderer/utils' +import { formatAgentServerError } from '@renderer/utils/error' import { AddAgentForm, AgentServerErrorSchema, @@ -43,9 +43,9 @@ const logger = loggerService.withContext('AgentApiClient') const processError = (error: unknown, fallbackMessage: string) => { logger.error(fallbackMessage, error as Error) if (isAxiosError(error)) { - const result = AgentServerErrorSchema.safeParse(error.response) + const result = AgentServerErrorSchema.safeParse(error.response?.data) if (result.success) { - return new Error(formatAgentServerError(result.data), { cause: error }) + return new Error(formatAgentServerError(result.data)) } } else if (error instanceof ZodError) { return error diff --git a/src/renderer/src/utils/api.ts b/src/renderer/src/utils/api.ts index 15129397d5..5e9b8f91a6 100644 --- a/src/renderer/src/utils/api.ts +++ b/src/renderer/src/utils/api.ts @@ -1,7 +1,3 @@ -import { AgentServerError } from '@renderer/types' -import { AxiosError } from 'axios' -import { t } from 'i18next' - /** * 格式化 API key 字符串。 * @@ -73,16 +69,3 @@ export function splitApiKeyString(keyStr: string): string[] { .map((k) => k.replace(/\\,/g, ',')) .filter((k) => k) } - -export const formatAgentServerError = (error: AgentServerError) => - `${t('common.error')}: ${error.error.code} ${error.error.message}` - -export const formatAxiosError = (error: AxiosError) => { - if (!error.response) { - return `${t('common.error')}: ${t('error.no_response')}` - } - - const { status, statusText } = error.response - - return `${t('common.error')}: ${status} ${statusText}` -} diff --git a/src/renderer/src/utils/error.ts b/src/renderer/src/utils/error.ts index ea8be21dc1..2336d15115 100644 --- a/src/renderer/src/utils/error.ts +++ b/src/renderer/src/utils/error.ts @@ -1,4 +1,5 @@ import { McpError } from '@modelcontextprotocol/sdk/types.js' +import { AgentServerError, AgentServerErrorSchema } from '@renderer/types' import { AiSdkErrorUnion, isSerializedAiSdkAPICallError, @@ -8,6 +9,7 @@ import { SerializedError } from '@renderer/types/error' import { InvalidToolInputError, NoSuchToolError } from 'ai' +import { AxiosError, isAxiosError } from 'axios' import { t } from 'i18next' import { z, ZodError } from 'zod' @@ -47,6 +49,13 @@ export function formatErrorMessage(error: unknown): string { if (error instanceof ZodError) { return formatZodError(error) } + if (isAxiosError(error)) { + return formatAxiosError(error) + } + const parseResult = AgentServerErrorSchema.safeParse(error) + if (parseResult.success) { + return formatAgentServerError(parseResult.data) + } const detailedError = getErrorDetails(error) delete detailedError?.headers delete detailedError?.stack @@ -294,3 +303,14 @@ export function formatAiSdkError(error: SerializedAiSdkError): string { return text.trim() } +export const formatAgentServerError = (error: AgentServerError) => + `${t('common.error')}: ${error.error.code} ${error.error.message}` +export const formatAxiosError = (error: AxiosError) => { + if (!error.response) { + return `${t('common.error')}: ${t('error.no_response')}` + } + + const { status, statusText } = error.response + + return `${t('common.error')}: ${status} ${statusText}` +}