From 7134437e00308ad971d3cd562e45d167255c922e Mon Sep 17 00:00:00 2001 From: snusxd Date: Tue, 14 Apr 2026 22:21:07 +0300 Subject: [PATCH] fix: improve error handling for iTunes API response and update cover not found response --- src/download_cover.ts | 10 ++++++++-- src/index.ts | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/download_cover.ts b/src/download_cover.ts index b5a8aef..f2cbcfe 100644 --- a/src/download_cover.ts +++ b/src/download_cover.ts @@ -40,14 +40,20 @@ export async function itunes_cover_url(artist: string, album: string) { .replace(/[-_,"':;|]/g, " ") .toLowerCase(); - const apple_music_results = await fetch("https://itunes.apple.com/search", { + const response = await fetch("https://itunes.apple.com/search", { body: new URLSearchParams({ term: query, entity: "song", limit: "10", }), method: "POST", - }).then((res) => res.json()); + }); + + if (!response.ok) { + throw new Error(`iTunes API failed with status ${response.status}`); + } + + const apple_music_results = await response.json(); for (let result of apple_music_results.results) { if (artist == result.artistName.toLowerCase()) { diff --git a/src/index.ts b/src/index.ts index f02b75e..540f221 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,7 @@ app.get("/cover/:artist/:album/:id", ({ params: { artist, album, id } }) => get_image_from_server(artist, album, id), ); -app.listen({ hostname: '0.0.0.0', port: 3000 }, ({ hostname, port }) => { +app.listen({ hostname: "0.0.0.0", port: 3000 }, ({ hostname, port }) => { console.log(`🦊 Elysia is running at ${hostname}:${port}`); }); @@ -74,7 +74,7 @@ async function get_image_from_server( first_error, secong_error, ); - throw Error("cover not found"); + return new Response("Cover not found", { status: 404 }); } } }