Skip to content

Commit

Permalink
fix: Ignore FetchError (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina committed Sep 25, 2021
1 parent ffd4e3c commit 0782204
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/bot/skin/github-notification-query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch, { FetchError } from "node-fetch";
import { GitHubNotifications } from "../exp/github-notification";
import { GitHubUser } from "../exp/github-user";
import { Query } from "../play/notify";
import fetch from "node-fetch";

export const notificationQuery: Query = {
async fetchNotification({
Expand All @@ -11,14 +11,21 @@ export const notificationQuery: Query = {
const base64 = Buffer.from(`${userName}:${notificationToken}`).toString(
"base64",
);
const rawRes = await fetch("https://api.github.com/notifications", {
headers: {
Authorization: `Basic ${base64}`,
},
});
if (!rawRes.ok) {
throw new Error("fail to fetch notifications");
try {
const rawRes = await fetch("https://api.github.com/notifications", {
headers: {
Authorization: `Basic ${base64}`,
},
});
if (!rawRes.ok) {
return [];
}
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
} catch (err: unknown) {
if (err instanceof FetchError) {
return [];
}
throw err;
}
return [...((await rawRes.json()) as unknown[])] as GitHubNotifications;
},
};

0 comments on commit 0782204

Please sign in to comment.