Skip to content

Commit fcc5b25

Browse files
authored
fix: improve toErrorMessage (#714)
1 parent f2162d6 commit fcc5b25

File tree

2 files changed

+152
-13
lines changed

2 files changed

+152
-13
lines changed

src/fetch-wrapper.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,22 @@ async function getResponseData(response: Response): Promise<any> {
168168
return response.arrayBuffer();
169169
}
170170

171-
function toErrorMessage(data: any) {
172-
if (typeof data === "string") return data;
173-
174-
let suffix: string;
171+
function toErrorMessage(data: string | ArrayBuffer | Record<string, unknown>) {
172+
if (typeof data === "string") {
173+
return data;
174+
}
175175

176-
if ("documentation_url" in data) {
177-
suffix = ` - ${data.documentation_url}`;
178-
} else {
179-
suffix = "";
176+
if (data instanceof ArrayBuffer) {
177+
return "Unknown error";
180178
}
181179

182180
if ("message" in data) {
183-
if (Array.isArray(data.errors)) {
184-
return `${data.message}: ${data.errors.map(JSON.stringify).join(", ")}${suffix}`;
185-
}
181+
const suffix =
182+
"documentation_url" in data ? ` - ${data.documentation_url}` : "";
186183

187-
return `${data.message}${suffix}`;
184+
return Array.isArray(data.errors)
185+
? `${data.message}: ${data.errors.map((v) => JSON.stringify(v)).join(", ")}${suffix}`
186+
: `${data.message}${suffix}`;
188187
}
189188

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

test/request-native-fetch.test.ts

+141-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
571571
});
572572

573573
it("422 error with details", async () => {
574-
expect.assertions(8);
574+
expect.assertions(9);
575575

576576
const request = await mockRequestHttpServer((req, res) => {
577577
expect(req.method).toBe("POST");
@@ -607,6 +607,9 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
607607
throw new Error("should not resolve");
608608
} catch (error) {
609609
expect(error.status).toEqual(422);
610+
expect(error.message).toEqual(
611+
'Validation Failed: {"resource":"Label","code":"invalid","field":"color"} - https://developer.github.com/v3/issues/labels/#create-a-label',
612+
);
610613
expect(error.response.headers["x-foo"]).toEqual("bar");
611614
expect(error.response.data.documentation_url).toEqual(
612615
"https://developer.github.com/v3/issues/labels/#create-a-label",
@@ -619,6 +622,114 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
619622
}
620623
});
621624

625+
it("422 error with details without documentation_url", async () => {
626+
expect.assertions(9);
627+
628+
const request = await mockRequestHttpServer((req, res) => {
629+
expect(req.method).toBe("POST");
630+
expect(req.url).toBe("/repos/octocat/hello-world/labels");
631+
expect(req.headers.accept).toBe("application/vnd.github.v3+json");
632+
expect(req.headers["user-agent"]).toBe(userAgent);
633+
634+
res.writeHead(422, {
635+
"Content-Type": "application/json; charset=utf-8",
636+
"X-Foo": "bar",
637+
});
638+
res.end(
639+
JSON.stringify({
640+
message: "Validation Failed",
641+
errors: [
642+
{
643+
resource: "Label",
644+
code: "invalid",
645+
field: "color",
646+
},
647+
],
648+
}),
649+
);
650+
});
651+
652+
try {
653+
await request("POST /repos/octocat/hello-world/labels", {
654+
name: "foo",
655+
color: "invalid",
656+
});
657+
throw new Error("should not resolve");
658+
} catch (error) {
659+
expect(error.status).toEqual(422);
660+
expect(error.message).toEqual(
661+
'Validation Failed: {"resource":"Label","code":"invalid","field":"color"}',
662+
);
663+
expect(error.response.headers["x-foo"]).toEqual("bar");
664+
expect(error.response.data.documentation_url).toEqual(undefined);
665+
expect(error.response.data.errors).toEqual([
666+
{ resource: "Label", code: "invalid", field: "color" },
667+
]);
668+
} finally {
669+
request.closeMockServer();
670+
}
671+
});
672+
673+
it("422 error with ArrayBuffer as body", async () => {
674+
expect.assertions(7);
675+
676+
const request = await mockRequestHttpServer((req, res) => {
677+
expect(req.method).toBe("POST");
678+
expect(req.url).toBe("/repos/octocat/hello-world/labels");
679+
expect(req.headers.accept).toBe("application/vnd.github.v3+json");
680+
expect(req.headers["user-agent"]).toBe(userAgent);
681+
682+
res.writeHead(422, {
683+
"Content-Type": "application/octet-stream",
684+
});
685+
res.end("payload");
686+
});
687+
688+
try {
689+
await request("POST /repos/octocat/hello-world/labels", {
690+
name: "foo",
691+
color: "invalid",
692+
});
693+
throw new Error("should not resolve");
694+
} catch (error) {
695+
expect(error.status).toEqual(422);
696+
expect(error.message).toEqual("Unknown error");
697+
expect(error.response.data).instanceOf(ArrayBuffer);
698+
} finally {
699+
request.closeMockServer();
700+
}
701+
});
702+
703+
it("422 error without body", async () => {
704+
expect.assertions(7);
705+
706+
const request = await mockRequestHttpServer((req, res) => {
707+
expect(req.method).toBe("POST");
708+
expect(req.url).toBe("/repos/octocat/hello-world/labels");
709+
expect(req.headers.accept).toBe("application/vnd.github.v3+json");
710+
expect(req.headers["user-agent"]).toBe(userAgent);
711+
712+
res.writeHead(422, {
713+
"Content-Type": "text/plain",
714+
});
715+
res.end();
716+
});
717+
718+
try {
719+
await request("POST /repos/octocat/hello-world/labels", {
720+
name: "foo",
721+
color: "invalid",
722+
});
723+
throw new Error("should not resolve");
724+
} catch (error) {
725+
expect(error.status).toEqual(422);
726+
expect(error.message).toEqual("");
727+
expect(error.response.data).toEqual("");
728+
} finally {
729+
request.closeMockServer();
730+
}
731+
});
732+
622733
it("redacts credentials from error.request.headers.authorization", async () => {
623734
expect.assertions(5);
624735

@@ -967,6 +1078,35 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
9671078
expect(error.response.data.documentation_url).toEqual(
9681079
"https://docs.github.com/en/rest/reference/repos#get-a-repository",
9691080
);
1081+
} finally {
1082+
request.closeMockServer();
1083+
}
1084+
});
1085+
1086+
it("404 not found without documentation_url", { skip: true }, async () => {
1087+
expect.assertions(3);
1088+
1089+
const request = await mockRequestHttpServer(async (req, res) => {
1090+
expect(req.method).toBe("GET");
1091+
expect(req.url).toBe("/repos/octocat/unknown");
1092+
1093+
res.writeHead(404);
1094+
res.end(
1095+
JSON.stringify({
1096+
message: "Not Found",
1097+
}),
1098+
);
1099+
});
1100+
1101+
try {
1102+
await request("GET /repos/octocat/unknown");
1103+
throw new Error("Should have thrown");
1104+
} catch (error) {
1105+
expect(error.status).toEqual(404);
1106+
expect(error.response.data.message).toEqual("Not Found");
1107+
expect(error.response.data.documentation_url).toEqual("");
1108+
} finally {
1109+
request.closeMockServer();
9701110
}
9711111
});
9721112

0 commit comments

Comments
 (0)