Skip to content

Commit bdaa3b1

Browse files
committed
chore: catch failed fetch requests to instatus api
1 parent 5d4f449 commit bdaa3b1

File tree

1 file changed

+28
-16
lines changed
  • frontend/src/ts/elements

1 file changed

+28
-16
lines changed

frontend/src/ts/elements/psa.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PSA } from "@monkeytype/contracts/schemas/psas";
88
import { z } from "zod";
99
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
1010
import { IdSchema } from "@monkeytype/contracts/schemas/util";
11+
import { tryCatch } from "@monkeytype/util/trycatch";
1112

1213
const confirmedPSAs = new LocalStorageWithSchema({
1314
key: "confirmedPSAs",
@@ -56,31 +57,42 @@ async function getLatest(): Promise<PSA[] | null> {
5657
url: string;
5758
updatedAt: string;
5859
}[];
59-
activeMaintenances: {
60-
id: string;
61-
name: string;
62-
start: string;
63-
status: "NOTSTARTEDYET" | "INPROGRESS" | "COMPLETED";
64-
duration: number;
65-
url: string;
66-
updatedAt: string;
67-
}[];
60+
activeMaintenances:
61+
| {
62+
id: string;
63+
name: string;
64+
start: string;
65+
status: "NOTSTARTEDYET" | "INPROGRESS" | "COMPLETED";
66+
duration: number;
67+
url: string;
68+
updatedAt: string;
69+
}[]
70+
| undefined;
6871
};
6972

70-
const instatus = await fetch(
71-
"https://monkeytype.instatus.com/summary.json"
73+
const { data: instatus, error } = await tryCatch(
74+
fetch("https://monkeytype.instatus.com/summary.json")
7275
);
73-
const instatusData =
74-
(await instatus.json()) as unknown as InstatusSummary;
7576

76-
const maintenanceData = instatusData.activeMaintenances[0];
77+
let maintenanceData: undefined | InstatusSummary["activeMaintenances"];
78+
79+
if (error) {
80+
console.log("Failed to fetch Instatus summary", error);
81+
} else {
82+
const instatusData =
83+
(await instatus.json()) as unknown as InstatusSummary;
84+
85+
maintenanceData = instatusData.activeMaintenances;
86+
}
7787

7888
if (
7989
maintenanceData !== undefined &&
80-
maintenanceData.status === "INPROGRESS"
90+
maintenanceData.length > 0 &&
91+
maintenanceData[0] !== undefined &&
92+
maintenanceData[0].status === "INPROGRESS"
8193
) {
8294
Notifications.addPSA(
83-
`Server is currently offline for scheduled maintenance. <a target= '_blank' href='${maintenanceData.url}'>Check the status page</a> for more info.`,
95+
`Server is currently offline for scheduled maintenance. <a target= '_blank' href='${maintenanceData[0].url}'>Check the status page</a> for more info.`,
8496
-1,
8597
"bullhorn",
8698
true,

0 commit comments

Comments
 (0)