From fd8446c33aca09014ef4d5971a7a9d232c46c9b6 Mon Sep 17 00:00:00 2001 From: Jack Murray-Brown Date: Thu, 6 Jun 2024 14:01:05 +0200 Subject: [PATCH] Bugfixes (#133) * improve error handling in live player * dont throw error in submission form if title is empty * fix show submission emails so it respects rate limit and doesn't end early on error --- components/livePlayer.tsx | 14 ++++++++- hooks/useSchedule.ts | 6 +++- lib/resend/email.ts | 2 +- pages/api/cron/show-submission-email.ts | 6 ++-- pages/api/schedule.ts | 38 +++++++++++++++++++------ pages/submission-v2.tsx | 2 +- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/components/livePlayer.tsx b/components/livePlayer.tsx index f46f430..e956cb4 100644 --- a/components/livePlayer.tsx +++ b/components/livePlayer.tsx @@ -71,8 +71,10 @@ export default function LivePlayer() { const { scheduleData, isLoading, error } = useSchedule(); + console.log(error); + const isOnline = scheduleData?.status === "online"; - const ch2IsOnline = scheduleData?.ch2.status === "online"; + const ch2IsOnline = scheduleData?.ch2?.status === "online"; const player = useRef(null); const source = useRef(null); @@ -122,6 +124,16 @@ export default function LivePlayer() { } }, [scheduleData]); + if (error) { + return ( +
+

+ Sorry, an error occurred. We will be back online shortly. +

+
+ ); + } + return (
` ); - throw new Error(error); } } diff --git a/pages/api/cron/show-submission-email.ts b/pages/api/cron/show-submission-email.ts index db6fc13..939c50e 100644 --- a/pages/api/cron/show-submission-email.ts +++ b/pages/api/cron/show-submission-email.ts @@ -59,15 +59,13 @@ async function sendEmails( for (const show of shows) { let showEmailed = false; - const emailPromises = show.artistsCollection.items.map(async (artist) => { + for (const artist of show.artistsCollection.items) { if (artist.email) { await sendEmail(artist, show, severity); showEmailed = true; await new Promise((resolve) => setTimeout(resolve, delay)); // Respect the rate limit } - }); - - await Promise.all(emailPromises); + } if (!showEmailed) { await sendSlackMessage( diff --git a/pages/api/schedule.ts b/pages/api/schedule.ts index c8e08f3..7998b75 100644 --- a/pages/api/schedule.ts +++ b/pages/api/schedule.ts @@ -33,13 +33,33 @@ export default async function handler( ) { try { const { data, duration } = await getScheduleData(); - const r = await fetch("https://public.radio.co/stations/s3699c5e49/status"); - const ch2 = await fetch( - "https://public.radio.co/stations/s8ce53d687/status" - ); - const radioCoData: RadioCo = await r.json(); - const radioCoDataCh2: RadioCo = await ch2.json(); - let liveNowArtwork = radioCoData.current_track.artwork_url; + let radioCoData: RadioCo; + let radioCoDataCh2: RadioCo; + + try { + const r = await fetch( + "https://public.radio.co/stations/s3699c5e49/status" + ); + + if (!r.ok) { + throw new Error(`HTTP error! status: ${r.status}`); + } + + radioCoData = await r.json(); + } catch (error) { + throw new Error(error.message); + } + + try { + const ch2 = await fetch( + "https://public.radio.co/stations/s8ce53d687/status" + ); + radioCoDataCh2 = await ch2.json(); + } catch (error) { + console.log("error loading channel 2: " + error.message); + } + + let liveNowArtwork = radioCoData?.current_track.artwork_url; const liveNowContentful = data.schedule.find((show) => { return show.live; }); @@ -74,8 +94,8 @@ export default async function handler( nextUp: data.nextUp, schedule: data.schedule, ch2: { - status: radioCoDataCh2.status, - liveNow: radioCoDataCh2.current_track.title, + status: radioCoDataCh2?.status, + liveNow: radioCoDataCh2?.current_track?.title, }, }; diff --git a/pages/submission-v2.tsx b/pages/submission-v2.tsx index 175bb26..736936c 100644 --- a/pages/submission-v2.tsx +++ b/pages/submission-v2.tsx @@ -106,7 +106,7 @@ function SubmissionForm({
); - if (!data.title || data.status == "TBC") + if (data.status == "TBC") return (