mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-04 22:32:12 +03:00
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:
committed by
GitHub
parent
e6e6d36907
commit
08acf86fe3
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user