From 410e8439c8fe609e5049a948fa6108b3bcb4e167 Mon Sep 17 00:00:00 2001 From: Georgy Butaev <41178744+g-but@users.noreply.github.com> Date: Sun, 7 Jun 2026 19:07:57 +0200 Subject: [PATCH] diag(entities): surface why STATUS endpoint returns 404 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Probed /api/entities/{entityType}/{id}/status PATCH end-to-end after the wishlist privacy fix and found the endpoint silently 404s for EVERY actor-owned entity tested (project, wishlist). It's been returning the same opaque "not found" since cfd98d60. Users who click Publish Now in the EntityCreationSuccess UI get a toast "Failed to publish" with no signal about why — could be missing row, could be RLS-blocked read, could be a bug in this route. The endpoint's `.single()` collapses all three into PGRST116. This commit doesn't fix the underlying 404 — root cause is still under investigation and likely lives in either the RLS policies (projects.user_id reference is suspicious despite being populated, wishlists owner read works via /api/wishlists/{id} but not here) or in this route's auth context propagation. What this commit DOES is make the failure mode legible: - Switch from .single() to .maybeSingle() so a non-existent row returns null cleanly instead of throwing an error that masks whether RLS-blocked vs missing. - Log fetchError details (code, message, details) on the error branch so the actual PostgREST error surfaces in vercel logs. - Log the "row not visible" branch separately with a note that it could be RLS-blocked OR row-missing — next time a user reports a Publish Now failure we can tell which. The behavior is unchanged for callers (still returns 404). What changes is that operators can now see, in logs, why. The actual root-cause fix needs the diagnostics to land first so the next person debugging this has something to grep for. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../[entityType]/[id]/status/route.ts | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/app/api/entities/[entityType]/[id]/status/route.ts b/src/app/api/entities/[entityType]/[id]/status/route.ts index 10aa0fc2b..0740f45d4 100644 --- a/src/app/api/entities/[entityType]/[id]/status/route.ts +++ b/src/app/api/entities/[entityType]/[id]/status/route.ts @@ -93,9 +93,32 @@ export const PATCH = withAuth(async (request: AuthenticatedRequest, context: Rou const { data: existing, error: fetchError } = await (supabase.from(tableName) as any) .select(`id, ${statusSelectColumn}, ${userIdField}`) .eq('id', id) - .single(); + .maybeSingle(); - if (fetchError || !existing) { + if (fetchError) { + // PostgREST + supabase-js collapses RLS-blocked-read into a generic + // 404-shaped error if we use `.single()`, hiding the actual failure + // mode. Surface the error code/details so this stops being invisible. + logger.error('Entity status: fetch failed', { + entityType, + id, + userId: user.id, + errorCode: fetchError.code, + errorMessage: fetchError.message, + errorDetails: fetchError.details, + }); + return apiNotFound(`${entityType} not found`); + } + if (!existing) { + // .maybeSingle returns null both for "row doesn't exist" AND "RLS + // blocked the read." Log so we can tell them apart when a user reports + // a Publish Now toast — historically these silently degraded to 404. + logger.warn('Entity status: row not visible to user', { + entityType, + id, + userId: user.id, + likelyCause: 'RLS-blocked or row missing', + }); return apiNotFound(`${entityType} not found`); }