refactor(error-handling): move error formatting functions to error utils

Consolidate error formatting functions (formatAgentServerError and formatAxiosError) into error.ts utility file to improve code organization and maintainability
This commit is contained in:
icarus 2025-09-20 00:32:43 +08:00
parent 8efafc6ba9
commit 825b5e1be4
3 changed files with 23 additions and 20 deletions

View File

@ -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

View File

@ -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}`
}

View File

@ -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}`
}