From 3202d0487159d6b9f07c387d04a139d794eb8cf6 Mon Sep 17 00:00:00 2001 From: Brent Vollebregt Date: Mon, 22 Apr 2024 18:03:32 +1200 Subject: [PATCH] Improve error logging for lrclib.net calls --- src/api/lrclib.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/api/lrclib.ts b/src/api/lrclib.ts index 971bae9..7a97e11 100644 --- a/src/api/lrclib.ts +++ b/src/api/lrclib.ts @@ -3,6 +3,8 @@ import { Lrc, Lyric } from "lrc-kit"; import { IFoundLyrics } from "../dto"; const LRCLIB_BASE_URL = "https://lrclib.net"; +const LRCLIB_USER_AGENT = + "Spotify Lyrics Viewer (https://github.com/brentvollebregt/spotify-lyrics-viewer)"; export async function getLyrics( artists: string[], @@ -26,6 +28,9 @@ export async function getLyrics( try { const response = await axios.get(requestUrl, { + headers: { + "User-Agent": LRCLIB_USER_AGENT + }, validateStatus: status => status === 200 || status === 404 }); @@ -63,10 +68,24 @@ export async function getLyrics( syncedLyrics: syncedLyrics, attribution: LRCLIB_BASE_URL } as IFoundLyrics; - } catch (e) { + } catch (e: unknown) { // Can include 404 and anything else non-2xx console.warn(`Failed to call '${requestUrl}'`); - console.warn(e); + + if (e instanceof Error && e.stack !== undefined) { + console.warn(e.stack); + } + + if (axios.isAxiosError(e)) { + if (e.response !== undefined) { + console.log(`Response: HTTP${e.response.status} ${e.response.statusText}`); + console.log(`Response headers: ${JSON.stringify(e.response.headers)}`); + console.log(`Response data: ${JSON.stringify(e.response.data)}`); + } + } else { + console.warn(e); + } + return null; } }