Add logs to spotify based APIs to help trace random network failure errors better

vielle.dev 2c4647b1 a3314bb6

verified
Changed files
+14 -12
src
components
home
pages
+2 -2
src/components/home/playing/NowPlaying.astro
··· 14 14 if (!(err instanceof SpotifyError)) throw new Error("Unhandled exception"); 15 15 if (err.code === "NO_CONTENT") return null; 16 16 17 - console.error(err.code, err.human, err.details); 17 + console.error("NowPlaying.astro:", err.code, err.human, err.details); 18 18 return err; 19 19 }); 20 20 21 21 if (track instanceof SpotifyError) 22 - console.error("Encountered spotify error:", track); 22 + console.error("NowPlaying.astro:", "Encountered spotify error:", track); 23 23 24 24 const dataTrack = ( 25 25 t: typeof track,
+5 -5
src/components/home/playing/spotify/access.ts
··· 103 103 // SyntaxError 104 104 // Response 105 105 // SpotifyError<"INVALID_AUTH_RES"> 106 - if (err instanceof Response) console.error("Request failed:", err); 106 + if (err instanceof Response) console.error("access.ts", "Request failed:", err); 107 107 else if (err instanceof SyntaxError) 108 - console.error("Response JSON failed", err); 108 + console.error("access.ts", "Response JSON failed", err); 109 109 else if (err instanceof SpotifyError && err.code === "INVALID_AUTH_RES") 110 - console.error("Response malformed:", err); 110 + console.error("access.ts", "Response malformed:", err); 111 111 else if (err instanceof TypeError) { 112 - console.error("A network error occurred.", err); 112 + console.error("access.ts", "A network error occurred.", err); 113 113 return new SpotifyError( 114 114 "NETWORK_ERR", 115 115 err, 116 116 "Network error occurred. Could not reach spotify servers or something else.", 117 117 ); 118 118 } else { 119 - console.error("Unhandled exception."); 119 + console.error("access.ts", "Unhandled exception."); 120 120 throw err; 121 121 } 122 122 })
+2 -2
src/components/home/playing/spotify/api.ts
··· 55 55 // auth failed 56 56 nowPlaying.catch((err) => { 57 57 if (err instanceof SpotifyError && err.code === "NO_AUTH") { 58 - console.error("Authentication failed:", err.human); 58 + console.error("api.ts", "Authentication failed:", err.human); 59 59 rej(err); 60 60 } else if (err instanceof SpotifyError && err.code === "NETWORK_ERR") { 61 - console.error("Network request failed:", err.human); 61 + console.error("api.ts", "Network request failed:", err.human); 62 62 rej(err) 63 63 } 64 64 });
+5 -3
src/pages/now-playing-sse.ts
··· 38 38 throw new Error("Unhandled exception"); 39 39 if (err.code === "NO_CONTENT") return null; 40 40 41 - console.error(err.code, err.human, err.details); 41 + console.error("/now-playing-sse", err.code, err.human, err.details, JSON.stringify(err)); 42 42 return err; 43 43 }); 44 + 45 + console.log("SENDING:", playing && "name" in playing ? playing.name + " by " + playing.artists.map(artist => artist.name).join(", ") : playing) 44 46 45 47 // SSE syntax: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format 46 48 return `event: playing\ndata: ${JSON.stringify(playing)}\n\n`; ··· 56 58 update() 57 59 // dont write if aborted as it can cause errors 58 60 .then((val) => 59 - !abort.signal.aborted ? controller.enqueue(val) : undefined, 61 + !abort.signal.aborted ? (() => {console.log("sending sending frfr:", val); controller.enqueue(val); console.log("sent sent frfr:", val)})() : undefined, 60 62 ) 61 63 .catch((err) => { 62 - console.error("GOT ERROR:", err); 64 + console.error("/now-playing-sse", "GOT ERROR:", err); 63 65 }); 64 66 await new Promise((res, rej) => { 65 67 setTimeout(res, 5000);