From ab5c1b9e567116fc259f845fd360f8cd122e435c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sat, 30 Mar 2024 15:17:35 +0100 Subject: [PATCH] fix: Add error message when unable to generate feed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim HÄrek Andreassen --- lib/nrk.ts | 1 + routes/api/feeds/[seriesId].ts | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/nrk.ts b/lib/nrk.ts index c0e121c..9b284ec 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -47,6 +47,7 @@ export const nrkRadio = { searchComponents["schemas"]["searchresult"] >(`https://psapi.nrk.no/radio/search/search?q=${query}`); if (status === OK && response) { + console.log("res", response.results); return response.results.series?.results; } else { throw `Something went wrong with ${query} - got status ${status}`; diff --git a/routes/api/feeds/[seriesId].ts b/routes/api/feeds/[seriesId].ts index 2057c20..52732a4 100644 --- a/routes/api/feeds/[seriesId].ts +++ b/routes/api/feeds/[seriesId].ts @@ -88,11 +88,21 @@ async function buildFeed(seriesId: string) { export const handler = async (_req: Request, ctx: FreshContext): Promise => { const seriesId = ctx.params.seriesId; - const feedContent = await buildFeed(seriesId); + try { + const feedContent = await buildFeed(seriesId); - return new Response(feedContent, { - headers: { - "Content-Type": "application/xml", - }, - }); + return new Response(feedContent, { + headers: { + "Content-Type": "application/xml", + }, + }); + } catch (error) { + console.error(error); + return new Response(JSON.stringify({ message: "Could not generate feed" }), { + status: 500, + headers: { + "Content-Type": "application/json", + }, + }); + } };