From 31c28e7c88ceceb84437b46bf6ce6ff513b72120 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 15 Nov 2026 19:57:27 -0400 Subject: [PATCH 1/3] fix: preserve message body responses as text --- .../lib/api/resources/Messages.test.ts | 27 +++++++++++++++++++ src/lib/api/resources/Messages.ts | 10 +++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/__tests__/lib/api/resources/Messages.test.ts b/src/__tests__/lib/api/resources/Messages.test.ts index 85327311..ebe6ec63 100644 --- a/src/__tests__/lib/api/resources/Messages.test.ts +++ b/src/__tests__/lib/api/resources/Messages.test.ts @@ -377,6 +377,33 @@ describe("lib/api/resources/Messages: ", () => { }); }); + describe.each([ + ["getTextMessage", "txt"], + ["getRawMessage", "raw"], + ["getMessageHtmlSource", "htmlsource"], + ["getHtmlMessage", "html"], + ["getMessageAsEml", "eml"], + ] as const)("%s preserves response text", (method, extension) => { + it.each([ + "123", + "null", + "true", + '{"message":"hello"}', + '"hello"', + " 123\n", + "", + ])("returns %p unchanged", async (body) => { + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/inboxes/${sandboxId}/messages/${messageId}/body.${extension}`; + mock.onGet(endpoint).reply(200, body, { + "Content-Type": "text/plain", + }); + + const result = await messagesAPI[method](sandboxId, messageId); + + expect(result).toBe(body); + }); + }); + describe("getTextMessage(): ", () => { it("successfully gets text message.", async () => { const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/inboxes/${sandboxId}/messages/${messageId}/body.txt`; diff --git a/src/lib/api/resources/Messages.ts b/src/lib/api/resources/Messages.ts index d87f0df1..2c38a09b 100644 --- a/src/lib/api/resources/Messages.ts +++ b/src/lib/api/resources/Messages.ts @@ -109,7 +109,7 @@ export default class MessagesApi { public async getTextMessage(sandboxId: number, messageId: number) { const url = `${this.messagesURL}/${sandboxId}/messages/${messageId}/body.txt`; - return this.client.get(url); + return this.client.get(url, { responseType: "text" }); } /** @@ -118,7 +118,7 @@ export default class MessagesApi { public async getRawMessage(sandboxId: number, messageId: number) { const url = `${this.messagesURL}/${sandboxId}/messages/${messageId}/body.raw`; - return this.client.get(url); + return this.client.get(url, { responseType: "text" }); } /** @@ -127,7 +127,7 @@ export default class MessagesApi { public async getMessageHtmlSource(sandboxId: number, messageId: number) { const url = `${this.messagesURL}/${sandboxId}/messages/${messageId}/body.htmlsource`; - return this.client.get(url); + return this.client.get(url, { responseType: "text" }); } /** @@ -136,7 +136,7 @@ export default class MessagesApi { public async getHtmlMessage(sandboxId: number, messageId: number) { const url = `${this.messagesURL}/${sandboxId}/messages/${messageId}/body.html`; - return this.client.get(url); + return this.client.get(url, { responseType: "text" }); } /** @@ -145,7 +145,7 @@ export default class MessagesApi { public async getMessageAsEml(sandboxId: number, messageId: number) { const url = `${this.messagesURL}/${sandboxId}/messages/${messageId}/body.eml`; - return this.client.get(url); + return this.client.get(url, { responseType: "text" }); } /** From bc9d052032d66e91054034da62c91c95e26f4b5c Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 15 Nov 2026 20:07:04 -0400 Subject: [PATCH 2/3] fix: decode JSON errors from message text requests --- .../lib/api/resources/Messages.test.ts | 11 ++++++++++ src/lib/axios-logger.ts | 20 +++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/__tests__/lib/api/resources/Messages.test.ts b/src/__tests__/lib/api/resources/Messages.test.ts index ebe6ec63..ac037c8e 100644 --- a/src/__tests__/lib/api/resources/Messages.test.ts +++ b/src/__tests__/lib/api/resources/Messages.test.ts @@ -404,6 +404,17 @@ describe("lib/api/resources/Messages: ", () => { }); }); + it("extracts JSON errors from text message responses", async () => { + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/inboxes/${sandboxId}/messages/${messageId}/body.txt`; + mock.onGet(endpoint).reply(404, '{"error":"Not found"}', { + "Content-Type": "application/json", + }); + + await expect( + messagesAPI.getTextMessage(sandboxId, messageId) + ).rejects.toMatchObject({ message: "Not found" }); + }); + describe("getTextMessage(): ", () => { it("successfully gets text message.", async () => { const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/inboxes/${sandboxId}/messages/${messageId}/body.txt`; diff --git a/src/lib/axios-logger.ts b/src/lib/axios-logger.ts index 7fad97ba..e4740a3d 100644 --- a/src/lib/axios-logger.ts +++ b/src/lib/axios-logger.ts @@ -199,11 +199,23 @@ function extractErrorMessage(data: unknown, defaultMessage: string): string { * Context information (status code, URL, etc.) is available in error.cause. */ function buildErrorMessage(error: AxiosError): string { - const primaryMessage = error.response?.data - ? extractErrorMessage(error.response.data, error.message) - : error.message; + let data: unknown = error.response?.data; + const contentType = Object.entries(error.response?.headers || {}).find( + ([name]) => name.toLowerCase() === "content-type" + )?.[1]; - return primaryMessage; + if ( + typeof data === "string" && + /application\/(?:[\w.+-]+\+)?json\b/i.test(String(contentType)) + ) { + try { + data = JSON.parse(data); + } catch { + // Preserve the response text if the server returns malformed JSON. + } + } + + return data ? extractErrorMessage(data, error.message) : error.message; } /** From fb81df48a6a02450fcce775aae27f5353c33507c Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Nov 2026 04:41:47 -0400 Subject: [PATCH 3/3] fix: decode text error bodies by request responseType Co-Authored-By: Claude Sonnet 5.5 --- src/__tests__/lib/api/resources/Messages.test.ts | 11 +++++++++++ src/lib/axios-logger.ts | 14 +++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/__tests__/lib/api/resources/Messages.test.ts b/src/__tests__/lib/api/resources/Messages.test.ts index ac037c8e..67282143 100644 --- a/src/__tests__/lib/api/resources/Messages.test.ts +++ b/src/__tests__/lib/api/resources/Messages.test.ts @@ -415,6 +415,17 @@ describe("lib/api/resources/Messages: ", () => { ).rejects.toMatchObject({ message: "Not found" }); }); + it("extracts JSON errors sent as text/plain from text message responses", async () => { + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/inboxes/${sandboxId}/messages/${messageId}/body.txt`; + mock.onGet(endpoint).reply(401, '{"error":"Incorrect API token"}', { + "Content-Type": "text/plain", + }); + + await expect( + messagesAPI.getTextMessage(sandboxId, messageId) + ).rejects.toMatchObject({ message: "Incorrect API token" }); + }); + describe("getTextMessage(): ", () => { it("successfully gets text message.", async () => { const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/inboxes/${sandboxId}/messages/${messageId}/body.txt`; diff --git a/src/lib/axios-logger.ts b/src/lib/axios-logger.ts index e4740a3d..a05f9452 100644 --- a/src/lib/axios-logger.ts +++ b/src/lib/axios-logger.ts @@ -200,18 +200,14 @@ function extractErrorMessage(data: unknown, defaultMessage: string): string { */ function buildErrorMessage(error: AxiosError): string { let data: unknown = error.response?.data; - const contentType = Object.entries(error.response?.headers || {}).find( - ([name]) => name.toLowerCase() === "content-type" - )?.[1]; - if ( - typeof data === "string" && - /application\/(?:[\w.+-]+\+)?json\b/i.test(String(contentType)) - ) { + // Only decode bodies requested as raw text; axios already parsed the rest. + if (typeof data === "string" && error.config?.responseType === "text") { try { - data = JSON.parse(data); + const parsed: unknown = JSON.parse(data); + if (parsed && typeof parsed === "object") data = parsed; } catch { - // Preserve the response text if the server returns malformed JSON. + // Plain-text error body, keep as is. } }