Skip to content

fix(api): stop surfacing DB errors as "no surface/composition yet" - #40

Merged
github-actions[bot] merged 1 commit into
mainfrom
fix/surface-composition-error-swallowing
Sep 3, 2026
Merged

fix(api): stop surfacing DB errors as "no surface/composition yet"#40
github-actions[bot] merged 1 commit into
mainfrom
fix/surface-composition-error-swallowing

Conversation

@catomean

@catomean catomean commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • GET /api/surfaces and GET /api/compositions collapsed every Supabase error (a real DB outage, an RLS misconfiguration — not just "no row yet") into { success: false, data: null } with no error field.
  • useSurface/useComposition only throw when !success && error, so a genuine query failure resolved clean to data: null, indistinguishable from "you haven't saved one yet" — a user who already saved a surface could get silently bounced back to "define your surface" on a transient failure, believing their work vanished.
  • Switched both handlers from .single() to .maybeSingle() — already this codebase's convention for "zero rows is a valid outcome" (see ownsProject/ownsFigure) — which returns data: null with no error on zero rows, and still forwards error.message for genuine failures, matching every other route in the app.
  • Added a structural test (src/app/api/error-shape.test.ts) that walks every API route's NextResponse.json({ success: false, ... }) call site and fails on any missing an error field, in the same spirit as the existing mutations-report-failure.test.ts guard.

Test plan

  • npm run verify (lint + typecheck + vitest) — all green, 89/89 tests
  • Confirmed the new guard test fails against the pre-fix code (git stash + run) and passes after the fix
  • No manual click-through — no Supabase credentials in this environment to force a real query failure through the UI

🤖 Generated with Claude Code

GET /api/surfaces and /api/compositions collapsed every Supabase error
into { success: false, data: null } with no error field. useSurface and
useComposition only throw when success is false AND error is set, so a
real query failure — a transient outage, an RLS misconfiguration — was
indistinguishable from the legitimate "you haven't created one yet"
state. A user who'd already saved a surface, hitting a transient
failure, was silently bounced back to "define your surface" as if the
work had never happened.

Switch both handlers from .single() to .maybeSingle() (already the
project's convention for "zero rows is a valid outcome", per
ownsProject/ownsFigure), which resolves data: null with no error on
zero rows and still surfaces error.message for genuine failures — the
same shape every other route in the app already uses.

Added a structural test that walks every API route's
NextResponse.json({ success: false, ... }) call site and fails on any
missing an error field, so this class of bug can't come back by hand.
@github-actions
github-actions Bot merged commit b1e53ef into main Sep 3, 2026
1 check passed
@github-actions
github-actions Bot deleted the fix/surface-composition-error-swallowing branch September 3, 2026 04:06
github-actions Bot pushed a commit that referenced this pull request Sep 4, 2026
#43)

POST /api/compositions computed nextVersion from `{ data: existing } =
await supabase...single()` without checking `error`. `.single()` throws
on zero rows — the normal case for a project's first composition — so
a real query failure (RLS misconfig, DB outage) left `data` null just
like "no composition yet," silently defaulting nextVersion to 1 instead
of surfacing the failure and risking a colliding/duplicate version.

Switched to `.maybeSingle()` with an explicit error check, matching the
project's convention for "zero rows is valid" (ownsProject/ownsFigure,
the surfaces/compositions GET handlers from #40). Added a structural
test that walks every API route for a `data` destructure from a
supabase query missing its `error`, so a call site can't drop back to
this pattern.

Co-authored-by: Mao Nakamoto <maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
github-actions Bot pushed a commit that referenced this pull request Sep 5, 2026
)

Upsert deleted the existing surface before inserting the new one but
never checked the delete's result. If the delete silently failed (RLS
misconfig, transient DB error), the insert left two rows for one
project, and every later GET for that surface would 500 since it
relies on .maybeSingle() expecting at most one row.

Same unchecked-error family as PR #36/#37/#39/#40/#43.

Co-authored-by: Mao Nakamoto <maonakamoto@users.noreply.github.com>
github-actions Bot pushed a commit that referenced this pull request Sep 6, 2026
ownsProject/ownsFigure discarded the Supabase error from their select()
calls and returned false whenever data came back null — which is also
what happens on a real query failure (RLS misconfig, DB outage). Every
route's authorization check then reported "Not found" instead of
surfacing a server error, same bug class as #40 but one layer down in
the shared ownership helper, affecting every POST/PATCH/DELETE (and
several GETs) across surfaces, compositions, and figures.

ownsProject/ownsFigure now return { owns, error } so callers can tell
a real failure (500) apart from "caller doesn't own this" (404).

Co-authored-by: Mao Nakamoto <maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant