From bdaa3b16ff4393ecfd0e4bc0456beaaae0980c78 Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 30 Apr 2025 10:18:34 +0200 Subject: [PATCH] chore: catch failed fetch requests to instatus api --- frontend/src/ts/elements/psa.ts | 44 +++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/frontend/src/ts/elements/psa.ts b/frontend/src/ts/elements/psa.ts index 7f0fbe2dd..8446fa9d9 100644 --- a/frontend/src/ts/elements/psa.ts +++ b/frontend/src/ts/elements/psa.ts @@ -8,6 +8,7 @@ import { PSA } from "@monkeytype/contracts/schemas/psas"; import { z } from "zod"; import { LocalStorageWithSchema } from "../utils/local-storage-with-schema"; import { IdSchema } from "@monkeytype/contracts/schemas/util"; +import { tryCatch } from "@monkeytype/util/trycatch"; const confirmedPSAs = new LocalStorageWithSchema({ key: "confirmedPSAs", @@ -56,31 +57,42 @@ async function getLatest(): Promise { url: string; updatedAt: string; }[]; - activeMaintenances: { - id: string; - name: string; - start: string; - status: "NOTSTARTEDYET" | "INPROGRESS" | "COMPLETED"; - duration: number; - url: string; - updatedAt: string; - }[]; + activeMaintenances: + | { + id: string; + name: string; + start: string; + status: "NOTSTARTEDYET" | "INPROGRESS" | "COMPLETED"; + duration: number; + url: string; + updatedAt: string; + }[] + | undefined; }; - const instatus = await fetch( - "https://monkeytype.instatus.com/summary.json" + const { data: instatus, error } = await tryCatch( + fetch("https://monkeytype.instatus.com/summary.json") ); - const instatusData = - (await instatus.json()) as unknown as InstatusSummary; - const maintenanceData = instatusData.activeMaintenances[0]; + let maintenanceData: undefined | InstatusSummary["activeMaintenances"]; + + if (error) { + console.log("Failed to fetch Instatus summary", error); + } else { + const instatusData = + (await instatus.json()) as unknown as InstatusSummary; + + maintenanceData = instatusData.activeMaintenances; + } if ( maintenanceData !== undefined && - maintenanceData.status === "INPROGRESS" + maintenanceData.length > 0 && + maintenanceData[0] !== undefined && + maintenanceData[0].status === "INPROGRESS" ) { Notifications.addPSA( - `Server is currently offline for scheduled maintenance. Check the status page for more info.`, + `Server is currently offline for scheduled maintenance. Check the status page for more info.`, -1, "bullhorn", true,