logging and not crashing on dumb shit

This commit is contained in:
Wizzard 2023-07-14 12:54:16 -04:00
parent 8b7d2ef666
commit 94f1a8097a
1 changed files with 16 additions and 5 deletions

21
main.js
View File

@ -78,6 +78,8 @@ async function updateStatus() {
} }
async function fetchCurrentScrobble(user) { async function fetchCurrentScrobble(user) {
let lastTrackName;
let lastArtist;
try { try {
const optionsGetTrack = { const optionsGetTrack = {
uri: "http://ws.audioscrobbler.com/2.0/", uri: "http://ws.audioscrobbler.com/2.0/",
@ -93,8 +95,13 @@ async function fetchCurrentScrobble(user) {
const lastTrack = await fetch(optionsGetTrack); const lastTrack = await fetch(optionsGetTrack);
let lastArtist = lastTrack.recenttracks.track[0].artist["#text"]; if (!lastTrack.recenttracks.track[0]) {
let lastTrackName = lastTrack.recenttracks.track[0].name; console.error("No track data in recenttracks");
return null;
}
lastArtist = lastTrack.recenttracks.track[0].artist["#text"];
lastTrackName = lastTrack.recenttracks.track[0].name;
const options = { const options = {
uri: "http://ws.audioscrobbler.com/2.0/", uri: "http://ws.audioscrobbler.com/2.0/",
@ -111,6 +118,11 @@ async function fetchCurrentScrobble(user) {
const rData = await fetch(options); const rData = await fetch(options);
if (!rData.track) {
console.error("No track data in track.getInfo for track: " + lastTrackName + " by artist: " + lastArtist);
return null;
}
let images = lastTrack.recenttracks.track[0].image; let images = lastTrack.recenttracks.track[0].image;
let coverURL = images && images[images.length - 1]["#text"].trim() ? images[images.length - 1]["#text"].trim() : "default_cover"; let coverURL = images && images[images.length - 1]["#text"].trim() ? images[images.length - 1]["#text"].trim() : "default_cover";
@ -127,8 +139,7 @@ async function fetchCurrentScrobble(user) {
return data; return data;
} catch (error) { } catch (error) {
console.error("Failed to fetch current scrobble:", error); console.error("Failed to fetch current scrobble for track: " + lastTrackName + " by artist: " + lastArtist, error);
return null; return null;
} }
} }