fix(api): handle ZodError in processError and formatErrorMessage

Add explicit handling of ZodError in processError to return the error directly and in formatErrorMessage to use formatZodError for better error reporting
This commit is contained in:
icarus 2025-09-18 20:40:30 +08:00
parent ab90eb2aab
commit 7632efda88
2 changed files with 7 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import {
UpdateAgentResponseSchema UpdateAgentResponseSchema
} from '@types' } from '@types'
import { Axios, AxiosRequestConfig, isAxiosError } from 'axios' import { Axios, AxiosRequestConfig, isAxiosError } from 'axios'
import { ZodError } from 'zod'
type ApiVersion = 'v1' type ApiVersion = 'v1'
@ -29,6 +30,8 @@ const processError = (error: unknown, fallbackMessage: string) => {
if (result.success) { if (result.success) {
return new Error(formatAgentServerError(result.data), { cause: error }) return new Error(formatAgentServerError(result.data), { cause: error })
} }
} else if (error instanceof ZodError) {
return error
} }
return new Error(fallbackMessage, { cause: error }) return new Error(fallbackMessage, { cause: error })
} }

View File

@ -9,7 +9,7 @@ import {
} from '@renderer/types/error' } from '@renderer/types/error'
import { InvalidToolInputError, NoSuchToolError } from 'ai' import { InvalidToolInputError, NoSuchToolError } from 'ai'
import { t } from 'i18next' import { t } from 'i18next'
import { z } from 'zod' import { z, ZodError } from 'zod'
import { parseJSON } from './json' import { parseJSON } from './json'
import { safeSerialize } from './serialize' import { safeSerialize } from './serialize'
@ -44,6 +44,9 @@ export function getErrorDetails(err: any, seen = new WeakSet()): any {
} }
export function formatErrorMessage(error: unknown): string { export function formatErrorMessage(error: unknown): string {
if (error instanceof ZodError) {
return formatZodError(error)
}
const detailedError = getErrorDetails(error) const detailedError = getErrorDetails(error)
delete detailedError?.headers delete detailedError?.headers
delete detailedError?.stack delete detailedError?.stack