From 4ec6ae4d5838c9b6e5abc46f2f3bbf3f23e54d40 Mon Sep 17 00:00:00 2001 From: shibe <82057235+shiibe@users.noreply.github.com> Date: Wed, 28 Aug 2024 06:55:25 -0400 Subject: [PATCH] Enhance error handling and add artwork fallback to prevent freezes (#95) --- music-rpc.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/music-rpc.ts b/music-rpc.ts index 6858e91..0d281b7 100755 --- a/music-rpc.ts +++ b/music-rpc.ts @@ -27,11 +27,19 @@ while (true) { } async function main(rpc: Client) { - await rpc.connect(); - console.log(rpc); - while (true) { - const timeout = await setActivity(rpc); - await sleep(timeout); + try { + await rpc.connect(); + console.log("Connected to Discord RPC"); + while (true) { + const timeout = await setActivity(rpc); + await sleep(timeout); + } + } catch (err) { + console.error("Error in main loop:", err); + await rpc.close(); // Ensure the connection is properly closed + + console.log("Attempting to reconnect..."); + await sleep(DEFAULT_TIMEOUT); // wait before attempting to reconnect } } @@ -100,7 +108,18 @@ async function _getTrackExtras( entity: "song", term: query, }); - const resp = await fetch(`https://itunes.apple.com/search?${params}`); + const url = `https://itunes.apple.com/search?${params}`; + const resp = await fetch(url); + + if (!resp.ok) { + console.error("iTunes API error:", resp.statusText, url); + + return { + artworkUrl: await _getMBArtwork(artist, song, album) ?? null, + iTunesUrl: null, + }; + } + const json: iTunesSearchResponse = await resp.json(); let result: iTunesSearchResult | undefined; @@ -129,6 +148,7 @@ async function _getTrackExtras( result?.artworkUrl100 ?? (await _getMBArtwork(artist, song, album)) ?? null; const iTunesUrl = result?.trackViewUrl ?? null; + return { artworkUrl, iTunesUrl }; } //#endregion