Skip to content

Commit

Permalink
Improve error logging for lrclib.net calls
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvollebregt committed Apr 22, 2024
1 parent a597ad8 commit 3202d04
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/api/lrclib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand All @@ -26,6 +28,9 @@ export async function getLyrics(

try {
const response = await axios.get<LrcLibGetResponse>(requestUrl, {
headers: {
"User-Agent": LRCLIB_USER_AGENT
},
validateStatus: status => status === 200 || status === 404
});

Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 3202d04

Please sign in to comment.