fix(api): guard shared API client against non-JSON error responses (#5973)

Integrated into release/v3.8.44 — guard shared API client against non-JSON error responses.
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-07-03 00:19:06 -03:00
committed by GitHub
parent e6e6d36907
commit 08acf86fe3
2 changed files with 21 additions and 2 deletions

View File

@@ -96,10 +96,10 @@ export function getErrorMessage(
}
async function handleResponse(response: Response) {
const data = await response.json();
const data = await parseResponseBody(response);
if (!response.ok) {
const error: any = new Error(data.error || "An error occurred");
const error: any = new Error(getErrorMessage(data, response.status, "An error occurred"));
error.status = response.status;
error.data = data;
throw error;

View File

@@ -109,3 +109,22 @@ test("shared api utils throw enriched errors for non-OK responses", async () =>
}
);
});
test("shared api utils throw a clean error for non-JSON non-OK responses", async () => {
globalThis.fetch = async () =>
new Response("Bad Gateway", {
status: 502,
headers: { "Content-Type": "text/plain" },
});
await assert.rejects(
() => get("http://localhost/get"),
(error) => {
assert.ok(!(error instanceof SyntaxError), "must not be a raw JSON parse SyntaxError");
assert.match((error as any).message, /Bad Gateway/);
assert.equal((error as any).status, 502);
assert.equal((error as any).data, "Bad Gateway");
return true;
}
);
});