Skip to content

Commit

Permalink
Bugfixes (#133)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
antiantivirus authored Jun 6, 2024
1 parent df00f3c commit fd8446c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
14 changes: 13 additions & 1 deletion components/livePlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLAudioElement>(null);
const source = useRef<HTMLSourceElement>(null);
Expand Down Expand Up @@ -122,6 +124,16 @@ export default function LivePlayer() {
}
}, [scheduleData]);

if (error) {
return (
<section className={playerWrapperClassNames}>
<p className="p-2">
Sorry, an error occurred. We will be back online shortly.
</p>
</section>
);
}

return (
<section className={playerWrapperClassNames}>
<div
Expand Down
6 changes: 5 additions & 1 deletion hooks/useSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type Schedule = {
async function getSchedule(url: URL) {
const res = await fetch(url);

if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

return res.json();
}

Expand All @@ -36,6 +40,6 @@ export default function useSchedule() {
return {
scheduleData: data,
isLoading,
error: error,
error,
};
}
2 changes: 1 addition & 1 deletion lib/resend/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function sendEmail(artist, show, severity) {
showId: show.sys.id,
}),
});

if (error) {
throw new Error(error.name);
}
Expand All @@ -36,7 +37,6 @@ export async function sendEmail(artist, show, severity) {
sendSlackMessage(
`Failed to send email request to ${artist.name}(${artist.email}) on show *${show.title}*. ${error.name} - ${error.message}. <@U04HG3VHHEW>`
);
throw new Error(error);
}
}

Expand Down
6 changes: 2 additions & 4 deletions pages/api/cron/show-submission-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
38 changes: 29 additions & 9 deletions pages/api/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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,
},
};

Expand Down
2 changes: 1 addition & 1 deletion pages/submission-v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function SubmissionForm({
</div>
);

if (!data.title || data.status == "TBC")
if (data.status == "TBC")
return (
<div className="my-32">
<p>
Expand Down

0 comments on commit fd8446c

Please sign in to comment.