diff --git a/src/__tests__/lib/api/resources/Messages.test.ts b/src/__tests__/lib/api/resources/Messages.test.ts index 8532731..6728214 100644 --- a/src/__tests__/lib/api/resources/Messages.test.ts +++ b/src/__tests__/lib/api/resources/Messages.test.ts @@ -377,6 +377,55 @@ 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); + }); + }); + + 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" }); + }); + + 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/api/resources/Messages.ts b/src/lib/api/resources/Messages.ts index d87f0df..2c38a09 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" }); } /** diff --git a/src/lib/axios-logger.ts b/src/lib/axios-logger.ts index 7fad97b..a05f945 100644 --- a/src/lib/axios-logger.ts +++ b/src/lib/axios-logger.ts @@ -199,11 +199,19 @@ 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; + + // Only decode bodies requested as raw text; axios already parsed the rest. + if (typeof data === "string" && error.config?.responseType === "text") { + try { + const parsed: unknown = JSON.parse(data); + if (parsed && typeof parsed === "object") data = parsed; + } catch { + // Plain-text error body, keep as is. + } + } - return primaryMessage; + return data ? extractErrorMessage(data, error.message) : error.message; } /**