Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/createArtist.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { NEW_API_BASE_URL } from "@/lib/consts";

const createArtist = async (name: string, account_id: string) => {
try {
const response = await fetch(
`/api/artist/create?name=${encodeURIComponent(name)}&account_id=${account_id}`,
);
const response = await fetch(`${NEW_API_BASE_URL}/api/artist`, {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't check if the API response was successful before attempting to parse and access data.artist, which deviates from the established error-handling pattern used throughout the codebase. This could cause the function to return undefined instead of null when the API request fails with an HTTP error.

View Details
📝 Patch Details
diff --git a/lib/createArtist.tsx b/lib/createArtist.tsx
index 36e1565d..84f82698 100644
--- a/lib/createArtist.tsx
+++ b/lib/createArtist.tsx
@@ -9,10 +9,15 @@ const createArtist = async (name: string, account_id: string) => {
       },
       body: JSON.stringify({ name, account_id }),
     });
-    const data = await response.json();
 
+    if (!response.ok) {
+      return null;
+    }
+
+    const data = await response.json();
     return data.artist;
   } catch (error) {
+    console.error(error);
     return null;
   }
 };

Analysis

Missing response.ok check in createArtist() allows undefined return on API errors

What fails: lib/createArtist.tsx doesn't check response.ok before parsing JSON, causing the function to return undefined instead of null when the API returns HTTP error status codes with valid JSON responses.

How to reproduce:

// In lib/createArtist.tsx (before fix):
const createArtist = async (name: string, account_id: string) => {
  try {
    const response = await fetch(` 

The API endpoint (app/api/artist/create/route.ts) returns error responses without an artist field:

  • Status 400: { message: "Missing required parameters: name and account_id" }
  • Status 500: { message: "Failed to create artist" }
  • Status 400 on catch: { message: "..." }

Result: Function inconsistently returns undefined for API errors, while all 49+ similar API calls in the codebase use the response.ok pattern to return null on errors.

Expected: Function should check response.ok before attempting to parse response (following the established pattern in lib/getFanSegments.tsx, lib/getConversations.tsx, lib/getHandles.tsx, and 46+ other files).

Fix applied: Added if (!response.ok) return null; check after fetch, consistent with codebase patterns. Also restored console.error(error) logging in catch block for debugging visibility.

method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, account_id }),
});
const data = await response.json();

return data.artist;
} catch (error) {
console.error(error);
return null;
}
};
Expand Down
Loading