From 310e6111cfefe3364700fa37e954abbfce06a8d9 Mon Sep 17 00:00:00 2001 From: Joey Kraut Date: Tue, 14 Jan 2025 10:55:16 -0800 Subject: [PATCH] packages: core: http: Return raw response data if JSON cannot parse --- packages/core/src/utils/http.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/core/src/utils/http.ts b/packages/core/src/utils/http.ts index 96e2ca7..a298439 100644 --- a/packages/core/src/utils/http.ts +++ b/packages/core/src/utils/http.ts @@ -14,11 +14,23 @@ export async function postRelayerRaw(url: string, body: any, headers = {}) { try { const response = await axios.post(url, body, { headers, - transformResponse: (data) => parseBigJSON(data), + validateStatus: null, // Allow any status code to pass through + transformResponse: (data) => { + try { + return parseBigJSON(data) + } catch { + // If parsing fails, return raw data + return data + } + }, }) - // console.log(`POST ${url} with body: `, body, "response: ", response.data) - // Process the response data as needed - return response.data // Assuming the function should return the response data + + // For non-2xx responses, throw error with raw data + if (response.status < 200 || response.status >= 300) { + throw new BaseError(response.data) + } + + return response.data } catch (error) { if (axios.isAxiosError(error)) { if (error.response) {