From d5487ba6ac04d1d366fad2c87a597fbba2cf3743 Mon Sep 17 00:00:00 2001 From: Phantom <59059173+EurFelux@users.noreply.github.com> Date: Mon, 15 Sep 2025 16:41:03 +0800 Subject: [PATCH] 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. --- src/renderer/src/utils/error.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/utils/error.ts b/src/renderer/src/utils/error.ts index 024a6713b0..b69073e406 100644 --- a/src/renderer/src/utils/error.ts +++ b/src/renderer/src/utils/error.ts @@ -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