Beta Now Live!

Ship with confidence.

The AI inspector to ship faster.

Inspector General is the AI that helps any developer go from 1x developer to 10x developer.

Files changed (1) hide show
  1. src/server/api/routers/genius.ts +14 -8
src/server/api/routers/genius.ts CHANGED
@@ -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
- const url = `${ovhApiUrl}/v1/${data?.response?.song.primary_artist.name}/${data?.response?.song.title}`;
94
+ const url = `${ovhApiUrl}/v1/${data?.response?.song.primary_artist.name}/${data?.response?.song.title}`;
94
- const response = await fetch(url);
95
+ const response = await fetch(url);
95
- const res = await response.json() as { lyrics: string };
96
+ const res = await response.json() as { lyrics: string };
97
+ // console.log(res);
96
- lyrics = res.lyrics ?? "Lyrics not found";
98
+ lyrics = res.lyrics ?? "Lyrics not found";
97
- lyrics = (res.lyrics || "").replaceAll("\n\n", "\n").replaceAll("\\", "").replaceAll("\"","");
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.

  1. 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 }
  1. 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.

  2. 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.

  1. Consider using a dedicated logging library: Instead of using 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) }),
Automate your workflow

Pull Requests Auto-Reviewed

Get instant PR code reviews on Github to help develoeprs ship fast!

Pull Requests Auto-Reviewed
Zone in on feedback

Chat with Inspector General

Ask inspector general for more details feedback on your code, or narrow in to file specific feedback.

Chat with Inspector General
Help Developers

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.

Line by line PR feedback

Pricing

Use it for free for yourself, upgrade when your team needs advanced control.

Free$0/moBest for 1-5 users
One repository
Email support
30 day data retention
Priority support
Pro$10/moBest for 5-50 users
One repository
Multiple repository
Email support
30 day data retention
365 day data retention
Priority support
EnterpriseContact usBest for 50+ users
Multiple repository
Email support
Data retention
Priority support