Inspector General is the AI that helps any developer go from 1x developer to 10x developer.
@@ -3,9 +3,7 @@ import {
|
|
3
3
|
createTRPCRouter,
|
4
4
|
publicProcedure,
|
5
5
|
} from "~/server/api/trpc";
|
6
|
-
import { type GeniusSongReference, type GeniusHit } from "~/schema";
|
6
|
+
import { type GeniusSongReference, type GeniusHit } from "~/schema";
|
7
|
-
import {parse} from "node-html-parser";
|
8
|
-
|
9
7
|
const geniusBaseUrl = "https://api.genius.com";
|
10
8
|
const ovhApiUrl = "https://api.lyrics.ovh";
|
11
9
|
|
@@ -90,11 +88,19 @@ export const geniusRouter = createTRPCRouter({
|
|
90
88
|
let lyrics = "";
|
91
89
|
|
92
90
|
if(lyrics === "" && data?.response?.song?.primary_artist?.name && data?.response?.song?.title) {
|
91
|
+
// console.log("artist: ", data?.response?.song?.primary_artist?.name);
|
92
|
+
// console.log("title: ", data?.response?.song?.title);
|
93
|
+
try{
|
93
|
-
|
94
|
+
const url = `${ovhApiUrl}/v1/${data?.response?.song.primary_artist.name}/${data?.response?.song.title}`;
|
94
|
-
|
95
|
+
const response = await fetch(url);
|
95
|
-
|
96
|
+
const res = await response.json() as { lyrics: string };
|
97
|
+
// console.log(res);
|
96
|
-
|
98
|
+
lyrics = res.lyrics ?? "Lyrics not found";
|
97
|
-
|
99
|
+
lyrics = (res.lyrics || "").replaceAll("\n\n", "\n").replaceAll("\\", "").replaceAll("\"","");
|
100
|
+
} catch (err) {
|
101
|
+
console.log(err);
|
102
|
+
lyrics = "Lyrics not found";
|
103
|
+
}
|
98
104
|
} else {
|
99
105
|
lyrics = "Lyrics not found";
|
100
106
|
}
|
The pull request adds error handling for fetching lyrics from the ovh
API. This is a good improvement. However, the error handling could be more robust and informative.
Generic Error Handling: The catch (err)
block only logs the error. It should also include more context, such as the specific request URL that failed and potentially a more user-friendly error message to be returned to the client.
javascript } catch (err) { console.error("Error fetching lyrics from OVH API for URL:", url, err); lyrics = "Failed to retrieve lyrics. Please try again later."; //More user-friendly message }
Specific Error Handling (Optional): Consider handling specific HTTP error codes (e.g., 404 Not Found) differently. A 404 might indicate that the song/artist is not found in the OVH API, allowing for a more precise message to the user.
Avoid Unnecessary replaceAll
Calls: The multiple replaceAll
calls can be chained together for efficiency:
javascript lyrics = (res.lyrics || "").replaceAll(/\\|\n\n|"/g, "");
This single line replaces all occurrences of \
, \n\n
, and "
. The /g
flag ensures all occurrences are replaced.
console.log
and console.error
, consider using a structured logging library like pino
or winston
. This will allow for easier monitoring and debugging in production.The improved getSong
function incorporating these suggestions would look like this:
javascript getSong: publicProcedure .input(z.object({ api_path: z.string() })) .mutation(async ({ input }) => { // ... (rest of the code remains the same) if(lyrics === "" && data?.response?.song?.primary_artist?.name && data?.response?.song?.title) { try{ const url = `${ovhApiUrl}/v1/${data?.response?.song.primary_artist.name}/${data?.response?.song.title}`; const response = await fetch(url); if (!response.ok) { console.error(`OVH API request failed with status ${response.status} for URL: ${url}`); lyrics = `Failed to retrieve lyrics (HTTP ${response.status}). Please try again later.`; return; } const res = await response.json() as { lyrics: string }; lyrics = (res.lyrics || "").replaceAll(/\\|\n\n|"/g, ""); } catch (err) { console.error("Error fetching lyrics from OVH API for URL:", url, err); lyrics = "Failed to retrieve lyrics. Please try again later."; } } else { lyrics = "Lyrics not found"; } // ... (rest of the code remains the same) }),
Pull Requests Auto-Reviewed
Get instant PR code reviews on Github to help develoeprs ship fast!
Chat with Inspector General
Ask inspector general for more details feedback on your code, or narrow in to file specific feedback.
Line by line PR feedback
We don't just tell a long list of issues, we provide feedback line by line to help developers find the issues and fix them quickly.
Use it for free for yourself, upgrade when your team needs advanced control.