Personal Site

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

vielle.dev 2c4647b1 a3314bb6

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