Skip to content

fix(rest): map schema-mismatch & not-null driver errors to structured 4xx#1594

Merged
xuyushun441-sys merged 2 commits into
mainfrom
fix/rest-map-schema-errors
Jun 5, 2026
Merged

fix(rest): map schema-mismatch & not-null driver errors to structured 4xx#1594
xuyushun441-sys merged 2 commits into
mainfrom
fix/rest-map-schema-errors

Conversation

@xuyushun441-sys

Copy link
Copy Markdown
Contributor

What

mapDataError collapsed any SQL-looking driver error into a generic 500 DATABASE_ERROR. A bad write payload to the data API therefore leaked a 500 instead of a fixable 4xx — e.g. POST /api/v1/data/sys_team with an unknown label field, or omitting required organization_id.

Adds two structured branches before the unknown-object / SQL-leak fallbacks:

Driver error (SQLite / Postgres / MySQL) Before After
unknown column (has no column named / no such column / column … of relation … does not exist / Unknown column) 500 DATABASE_ERROR 400 INVALID_FIELD { field, object }
not-null (NOT NULL constraint failed / null value in column / cannot be null) 500 DATABASE_ERROR 400 VALIDATION_FAILED { fields:[{required}] }

The unknown-column branch is placed before the unknown-object branch so Postgres column "x" of relation "y" does not exist isn't mis-mapped to 404 object_not_found. Genuine driver faults (SQLITE_IOERR) still → 500; unique violations still → 409 (unchanged).

Why

Found running cloud LOCAL-E2E-CHECKLIST B7 (per-env data API CRUD). This is a last-resort safety net — the durable fixes are upstream validation, tracked in #1590 (reject unknown fields), #1591 (enforce managedBy), #1592 (provenance-aware required checks). This PR closes the 500-leak symptom now.

Test

  • 8 new unit tests for mapDataError (SQLite/Postgres/MySQL phrasings, both error classes, plus genuine-leak-still-500 and unique-still-409 guards).
  • Full @objectstack/rest suite: 86/86 green. tsup build clean.
  • mapDataError exported package-internally (not added to index.ts) for testability.

Closes none directly; unblocks B7.

🤖 Generated with Claude Code

… 4xx

`mapDataError` collapsed any SQL-looking driver error into a generic
500 `DATABASE_ERROR`, so a bad write payload — e.g. `POST /data/sys_team`
with an unknown `label` field, or omitting the required `organization_id`
— leaked a 500 instead of a fixable 4xx. (Found running LOCAL-E2E-CHECKLIST
B7: per-env data API CRUD.)

Add two structured branches before the unknown-object / SQL-leak fallbacks:
- unknown column  → 400 INVALID_FIELD { field }  (SQLite "has no column
  named" / "no such column"; Postgres 'column "c" of relation ... does not
  exist'; MySQL "Unknown column 'c'"). Placed before the unknown-object
  branch so the Postgres phrasing isn't mis-mapped to 404 object_not_found.
- not-null        → 400 VALIDATION_FAILED { fields:[{required}] } (SQLite
  "NOT NULL constraint failed: t.c"; Postgres "null value in column";
  MySQL "Column 'c' cannot be null").

Genuine driver faults (e.g. SQLITE_IOERR) and unique violations are
unchanged (500 DATABASE_ERROR / 409 UNIQUE_VIOLATION). This is a
last-resort safety net; the durable fix is upstream validation
(unknown-field rejection + provenance-aware required checks) — tracked
separately.

Export `mapDataError` (package-internal; not added to index) + 8 tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jun 5, 2026 2:17am

Request Review

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@xuyushun441-sys xuyushun441-sys merged commit e1478fe into main Jun 5, 2026
7 of 8 checks passed
@xuyushun441-sys xuyushun441-sys deleted the fix/rest-map-schema-errors branch June 5, 2026 02:13
@github-actions github-actions Bot added size/m documentation Improvements or additions to documentation tooling labels Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/m tests tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants