fix(error): improve error response body parsing and message handling (#10181)

* fix(error): improve error response body parsing and message handling

Handle JSON parsing of error response bodies and extract internal messages when available. Combine messages when both top-level and internal messages exist.

* refactor(error): simplify response body assignment in serializeError

Remove redundant conditional logic and directly assign error.responseBody to serializedError.responseBody

* fix(serializeError): handle responseBody assignment consistently

Ensure responseBody is always assigned from error.responseBody when available, otherwise stringify the body. This prevents potential undefined behavior when error.responseBody exists but body is not available.
This commit is contained in:
Phantom 2025-09-15 16:41:03 +08:00 committed by GitHub
parent 95ed17aa69
commit d5487ba6ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,6 +11,7 @@ import { InvalidToolInputError, NoSuchToolError } from 'ai'
import { t } from 'i18next'
import { z } from 'zod'
import { parseJSON } from './json'
import { safeSerialize } from './serialize'
// const logger = loggerService.withContext('Utils:error')
@ -126,7 +127,23 @@ export const serializeError = (error: AiSdkErrorUnion): SerializedError => {
if ('url' in error) serializedError.url = error.url
if ('requestBodyValues' in error) serializedError.requestBodyValues = safeSerialize(error.requestBodyValues)
if ('statusCode' in error) serializedError.statusCode = error.statusCode ?? null
if ('responseBody' in error) serializedError.responseBody = error.responseBody ?? null
if ('responseBody' in error && error.responseBody) {
const body = parseJSON(error.responseBody)
if (body) {
// try to parse internal msg
const message = body.message || body.msg
if (message) {
if (serializedError.message === null) {
serializedError.message = message
} else {
serializedError.message += ' ' + message
}
}
serializedError.responseBody = JSON.stringify(body, null, 2)
} else {
serializedError.responseBody = error.responseBody
}
}
if ('isRetryable' in error) serializedError.isRetryable = error.isRetryable
if ('data' in error) serializedError.data = safeSerialize(error.data)
if ('responseHeaders' in error) serializedError.responseHeaders = error.responseHeaders ?? null