fix: replace .single() with .maybeSingle() in 5 API routes#475
Conversation
Prevents crashes when Supabase queries return 0 or multiple rows. .single() throws on empty results; .maybeSingle() returns null gracefully. Fixed routes: - GET/PUT /api/profile: handle missing profiles - POST/GET /api/applications: handle missing applications - POST/GET /api/gigs: handle missing gigs - POST /api/conversations: handle missing conversations - POST /api/messages/send: handle missing messages
Greptile SummaryThis PR replaces
Confidence Score: 3/5Two routes introduce new null-dereference crashes that silently degrade into 500 responses; needs targeted fixes before merging. The profile route's error handler now calls src/app/api/profile/route.ts (both GET and PUT handlers) and src/app/api/applications/route.ts (POST handler after INSERT). Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[API Request] --> B{getAuthContext}
B -->|Unauthorized| C[401 Response]
B -->|Authorized| D[Supabase Query]
D --> E{maybeSingle result}
E -->|data + no error| F[Success Path]
E -->|null + no error| G{Null Guard}
E -->|null + error| H[Error Response]
G -->|profile/route.ts incorrect guard| I["error.message → TypeError! error is null"]
I --> J[Catch block → 500]
G -->|conversations, messages, gigs correct guard| K[404 / meaningful error]
G -->|applications/route.ts missing guard on INSERT| L["application.id → TypeError! application is null"]
L --> M[Catch block → 500]
F --> N[JSON Response]
|
| if (error || !profile) { | ||
| return NextResponse.json({ error: error.message }, { status: 400 }); | ||
| } |
There was a problem hiding this comment.
Null dereference on
error.message when profile not found
With .maybeSingle(), when the query returns no rows, error is null and profile is null. The guard if (error || !profile) correctly catches the "no profile" case, but then error.message throws a TypeError because error is null. This uncaught exception propagates to the catch block, returning a generic 500 "An unexpected error occurred" response instead of the intended 400. The same pattern is broken in the PUT handler at line 125–129.
| ) | ||
| .eq("status", "active"); | ||
|
|
||
| // Apply filters — use textSearch or individual filters to prevent PostgREST filter injection (#71) | ||
| // Apply filters �?use textSearch or individual filters to prevent PostgREST filter injection (#71) |
There was a problem hiding this comment.
Comment encoding corruption (em dash →
?)
Three comments in this file had their em dash character (—) corrupted to ? (e.g., // Apply filters ?use textSearch…, // No filter ?return both types, // Apply pagination ?ensure non-negative offset…). These are likely a text-encoding side-effect of the PR tooling. The comments should be restored to their original readable form.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Replaced .single() with .maybeSingle() in profile, applications, gigs, conversations, and messages APIs to prevent crashes when queries return 0 results.
5 files changed, 28 insertions, 28 deletions.
Fixed routes: