Skip to content

Commit 554e102

Browse files
authored
fix: handle primitive JSON error bodies without throwing (#820)
When an HTTP error response has `content-type: application/json` but the JSON body is a primitive (`null`, a number, a boolean), `toErrorMessage()` threw a raw `TypeError` from `"message" in data` before `RequestError` was constructed, so callers lost the `status`, `request`, and `response` fields. Guard the object-shaped branch with `typeof data === "object" && data !== null` so primitives fall through to the generic `Unknown error: ...` message and a proper `RequestError` is thrown. Also widen the parameter type to `unknown`, which reflects that the parsed body can be any JSON value. Fixes #819
1 parent 14cf144 commit 554e102

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎src/fetch-wrapper.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function isJSONResponse(mimetype: ContentType): boolean {
190190
);
191191
}
192192

193-
function toErrorMessage(data: string | ArrayBuffer | Record<string, unknown>) {
193+
function toErrorMessage(data: unknown) {
194194
if (typeof data === "string") {
195195
return data;
196196
}
@@ -199,13 +199,20 @@ function toErrorMessage(data: string | ArrayBuffer | Record<string, unknown>) {
199199
return "Unknown error";
200200
}
201201

202-
if ("message" in data) {
202+
// Guard the object-shaped branch before using the `in` operator: a JSON error
203+
// body can be a primitive (`null`, a number, a boolean), and `"message" in
204+
// data` throws a raw TypeError on non-object values. Primitives fall through
205+
// to the generic unknown-error message below. See #819.
206+
if (typeof data === "object" && data !== null && "message" in data) {
207+
const objectData = data as Record<string, unknown>;
203208
const suffix =
204-
"documentation_url" in data ? ` - ${data.documentation_url}` : "";
209+
"documentation_url" in objectData
210+
? ` - ${objectData.documentation_url}`
211+
: "";
205212

206-
return Array.isArray(data.errors)
207-
? `${data.message}: ${data.errors.map((v) => JSON.stringify(v)).join(", ")}${suffix}`
208-
: `${data.message}${suffix}`;
213+
return Array.isArray(objectData.errors)
214+
? `${objectData.message}: ${objectData.errors.map((v) => JSON.stringify(v)).join(", ")}${suffix}`
215+
: `${objectData.message}${suffix}`;
209216
}
210217

211218
return `Unknown error: ${JSON.stringify(data)}`;

‎test/request.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,38 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
12931293
).rejects.toHaveProperty("message", "Unknown error: {}");
12941294
});
12951295

1296+
it("primitive JSON error bodies should result in a RequestError, not a raw TypeError", async () => {
1297+
// Regression test for #819: an error response with content-type
1298+
// application/json but a primitive body (null, a number, a boolean) used to
1299+
// throw a raw TypeError from `"message" in data` before RequestError was
1300+
// constructed, losing the status/request/response fields.
1301+
const cases: Array<[string, string]> = [
1302+
["null", "Unknown error: null"],
1303+
["42", "Unknown error: 42"],
1304+
["false", "Unknown error: false"],
1305+
];
1306+
1307+
expect.assertions(cases.length * 4);
1308+
1309+
for (const [body, expectedMessage] of cases) {
1310+
const fakeFetch = async () =>
1311+
new Response(body, {
1312+
status: 400,
1313+
headers: { "content-type": "application/json" },
1314+
});
1315+
1316+
const error: any = await request("GET /demo", {
1317+
baseUrl: "https://request-errors-test.com",
1318+
request: { fetch: fakeFetch },
1319+
}).catch((error) => error);
1320+
1321+
expect(error.name).toEqual("HttpError");
1322+
expect(error.status).toEqual(400);
1323+
expect(error.message).toEqual(expectedMessage);
1324+
expect(error.response).toBeDefined();
1325+
}
1326+
});
1327+
12961328
it("parses response bodies as JSON when Content-Type is application/scim+json", async () => {
12971329
expect.assertions(1);
12981330

0 commit comments

Comments
 (0)