diff --git a/src/bot/skin/github-notification-query.ts b/src/bot/skin/github-notification-query.ts index 97e2208..d95f6c9 100644 --- a/src/bot/skin/github-notification-query.ts +++ b/src/bot/skin/github-notification-query.ts @@ -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({ @@ -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; }, };