From 07822041add083fd629f608e5dbfd235676ec9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikuro=E3=81=95=E3=81=84=E3=81=AA?= Date: Sat, 25 Sep 2021 11:40:30 +0900 Subject: [PATCH] fix: Ignore FetchError (#41) --- src/bot/skin/github-notification-query.ts | 25 +++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) 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; }, };