Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/shared/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/shared-api-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);
});
Loading