fix(data-bbs): map 23505 UniqueConstraintViolation to JSON-RPC 409 (#508) - #511
Open
soberm wants to merge 1 commit into
Open
fix(data-bbs): map 23505 UniqueConstraintViolation to JSON-RPC 409 (#508)#511soberm wants to merge 1 commit into
soberm wants to merge 1 commit into
Conversation
) Follow-up to PR #500. Duplicate-key / unique-constraint violations (SQLSTATE 23505) were thrown as plain named Errors, which the JSON-RPC serializer maps to code 500 — the same "5xx for a 4xx cause" smell PR #500 fixed for OCC/40001. A duplicate key is a client-caused conflict, so it now returns 409 (Conflict). - bb-data: new uniqueConstraintConflict(cause) helper (ApiError, 409, name DatabaseErrors.UniqueConstraintViolation, fixed message, raw error kept only as server-side cause, not retriable). Routed through it in pg-error-translator, pglite-engine, and data-api-engine (both the SQLState-parsed and message-matched 23505 paths). - bb-distributed-data: translateDsqlError's 23505 branch now throws the equivalent 409 ApiError (mirrors its existing 40001 branch). error.name is preserved end-to-end so isBlocksError() keeps matching. ConnectionFailed / QueryFailed and other genuine infra errors correctly stay 500; the 40001 serialization path is unchanged. Behavior change: these paths now return 409 instead of 500. Refs #508.
🦋 Changeset detectedLatest commit: 1e199f0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Follow-up to #500. Duplicate-key / unique-constraint violations (SQLSTATE
23505,UniqueConstraintViolation) were thrown as plain namedErrors, so the JSON-RPC serializer (packages/core/src/rpc.ts) mapped them to code 500 — the same "5xx for a 4xx cause" smell #500 fixed for OCC/40001. A duplicate key is a client-caused conflict, so it should be 409 (Conflict).Issue #, if available: #508
Changes
Mirrors the OCC→409 pattern from #500.
@aws-blocks/bb-data— newuniqueConstraintConflict(cause)helper (parallel toserializationConflict) returningApiError('The item violates a unique constraint', 409, { name: DatabaseErrors.UniqueConstraintViolation, cause }). Routed through it at every23505site:pg-error-translator.ts,pglite-engine.ts, anddata-api-engine.ts(both the SQLState-parsedcode === '23505'path and the message-matched duplicate-key path).@aws-blocks/bb-distributed-data—translateDsqlError's23505branch now throws the equivalent 409ApiError(mirrors its existing40001branch).error.nameis preserved end-to-end soisBlocksError(e, DatabaseErrors.UniqueConstraintViolation)keeps matching on both server and client. The raw driver error is kept only as the server-sidecause(non-enumerable viaApiError; the serializer never emits it), and the client-visible message is a fixed string (no column/driver text leak).retriableisfalse— a blind retry of a duplicate insert fails identically.Out of scope / unchanged:
ConnectionFailed,QueryFailedand other genuine infra errors correctly stay 500; the40001serialization/OCC path (already 409 from #500) is untouched.Validation
Error/name-only assertions to assertApiError+status === 409+ preserved name (pg-error-translator.test.ts,pglite-engine.test.ts,data-api-engine.test.ts×3,bb-distributed-data/errors.test.ts) — confirmed red before the fix, green after.test-apps/comprehensive/test/database.test.tsanddsql.test.tsprovoke a real duplicate-key insert and asserte instanceof ApiError && e.status === 409 && isBlocksError(e, …UniqueConstraintViolation). No type casts. The PGlite/DSQL mocks enforce the primary key, so the path is genuinely exercised locally.npm run build,lint,lint:deps, changed-package unit tests (bb-data 381/0, bb-distributed-data 164/0), and bothchangeset-guardchecks (verify-coverage,verify-umbrella) pass..changeset/unique-violation-409.md—@aws-blocks/bb-data+@aws-blocks/bb-distributed-dataminor(pre-1.0 breaking = minor);@aws-blocks/blockspatch(umbrella).Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.