Skip to content

Commit 0782204

Browse files
authored
fix: Ignore FetchError (#41)
1 parent ffd4e3c commit 0782204

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import fetch, { FetchError } from "node-fetch";
12
import { GitHubNotifications } from "../exp/github-notification";
23
import { GitHubUser } from "../exp/github-user";
34
import { Query } from "../play/notify";
4-
import fetch from "node-fetch";
55

66
export const notificationQuery: Query = {
77
async fetchNotification({
@@ -11,14 +11,21 @@ export const notificationQuery: Query = {
1111
const base64 = Buffer.from(`${userName}:${notificationToken}`).toString(
1212
"base64",
1313
);
14-
const rawRes = await fetch("https://api.github.com/notifications", {
15-
headers: {
16-
Authorization: `Basic ${base64}`,
17-
},
18-
});
19-
if (!rawRes.ok) {
20-
throw new Error("fail to fetch notifications");
14+
try {
15+
const rawRes = await fetch("https://api.github.com/notifications", {
16+
headers: {
17+
Authorization: `Basic ${base64}`,
18+
},
19+
});
20+
if (!rawRes.ok) {
21+
return [];
22+
}
23+
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
24+
} catch (err: unknown) {
25+
if (err instanceof FetchError) {
26+
return [];
27+
}
28+
throw err;
2129
}
22-
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
2330
},
2431
};

0 commit comments

Comments
 (0)