From 8d41e1c5b341fff71da0c845bda1aba949ebbe29 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Thu, 22 Feb 2024 09:06:44 -0800 Subject: [PATCH] fix: improve now playing error handling --- netlify/edge-functions/now-playing.js | 56 +++++++++++++------- package.json | 2 +- src/assets/scripts/components/now-playing.js | 15 ++++-- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/netlify/edge-functions/now-playing.js b/netlify/edge-functions/now-playing.js index 310fdd16..be0623a7 100644 --- a/netlify/edge-functions/now-playing.js +++ b/netlify/edge-functions/now-playing.js @@ -69,10 +69,13 @@ export default async () => { }, }) .then((data) => { - if (data.body) return data.json(); - return {}; + if (data.ok) return data.json(); + throw new Error('Something went wrong with the Trakt endpoint.'); }) - .catch(); + .catch(err => { + console.log(err); + return {} + }); if (Object.keys(traktRes).length) { if (traktRes["type"] === "episode") { @@ -97,8 +100,14 @@ export default async () => { const nbaRes = await fetch( "https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json" ) - .then((data) => data.json()) - .catch(); + .then((data) => { + if (data.ok) return data.json() + throw new Error('Something went wrong with the NBA endpoint.'); + }) + .catch(err => { + console.log(err); + return {} + }); const games = nbaRes?.scoreboard?.games; if (games && games.length) { @@ -156,20 +165,27 @@ export default async () => { { type: "json", } - ).catch(); - const trackData = await trackRes.json(); + ).then((data) => { + if (data.ok) return data.json() + throw new Error('Something went wrong with the Last.fm endpoint.'); + }).catch(err => { + console.log(err); + return {} + }); const mbidRes = await fetch("https://coryd.dev/api/mbids", { type: "json", - }).catch(); - const mbidData = await mbidRes.json(); - const track = trackData["recenttracks"]["track"][0]; + }).then((data) => { + if (data.ok) return data.json() + throw new Error('Something went wrong with the mbid endpoint.'); + }).catch(err => { + console.log(err); + return {} + }); + const track = trackRes["recenttracks"]["track"][0]; const artist = track["artist"]["#text"]; let mbid = track["artist"]["mbid"]; let genre = ""; - - const mbidMap = (artist) => { - return mbidData[artist.toLowerCase()] || ""; - }; + const mbidMap = (artist) => mbidRes[artist.toLowerCase()] || ""; // mbid mismatches if (mbidMap(artist) !== "") mbid = mbidMap(artist); @@ -188,10 +204,14 @@ export default async () => { const genreUrl = `https://musicbrainz.org/ws/2/artist/${mbid}?inc=aliases+genres&fmt=json`; const genreRes = await fetch(genreUrl, { type: "json", - }).catch(); - const genreData = await genreRes.json(); - genre = - genreData.genres.sort((a, b) => b.count - a.count)[0]?.["name"] || ""; + }).then((data) => { + if (data.ok) return data.json() + throw new Error('Something went wrong with the MusicBrainz endpoint.'); + }).catch(err => { + console.log(err); + return {} + }); + genre = genreRes['genres'].sort((a, b) => b.count - a.count)[0]?.["name"] || ""; } return Response.json( diff --git a/package.json b/package.json index a575573e..d76a9b4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "6.5.10", + "version": "6.5.11", "description": "The source for my personal site. Built using 11ty and hosted on Netlify.", "type": "module", "scripts": { diff --git a/src/assets/scripts/components/now-playing.js b/src/assets/scripts/components/now-playing.js index 8aa51547..51e69f59 100644 --- a/src/assets/scripts/components/now-playing.js +++ b/src/assets/scripts/components/now-playing.js @@ -24,10 +24,17 @@ class NowPlaying extends HTMLElement { const content = this.querySelector('[data-key="content"]') const value = data['content'] - loading.style.opacity = '0' - loading.style.display = 'none' - content.style.opacity = '1' - content.innerHTML = value + if (!value) { + loading.style.display = 'none' + content.style.display = 'none' + } + + if (value) { + loading.style.opacity = '0' + loading.style.display = 'none' + content.style.opacity = '1' + content.innerHTML = value + } } get template() {