Repository navigation
fix(api): Answer duplicate contacts with 409 instead of an empty 500 - #11
Merged
Merged
Conversation
POST /api/v1/lists/{listId}/contacts did a plain insert, so a contact already
in the list hit the (list_id, email) unique constraint and the uncaught error
became Next.js's empty-body 500. Integrations retry on 5xx, so those signups
were retried forever.
- Create contact: insert with onConflictDoNothing on (list_id, email) and
answer 409 in the v1 envelope with the existing contact's id, email and
status. No confirmation or verification jobs are queued for it.
- Post-insert side effects (confirmation job, verification job, audit log)
are each caught and logged so they cannot fail a request whose contact was
already created.
- withApiAuth now catches any error from auth, rate limiting or the handler,
logs it, and answers a JSON 500 ({ data: null, meta: {}, error }) for every
v1 route.
- Update contact (PUT): changing the email to one another contact in the list
has now answers 409 instead of 500.
- Internal create contact: duplicate detection matched 'unique' in the error
message, but drizzle-orm wraps driver errors and puts the SQL there, so it
never matched. Check SQLSTATE 23505 on the error or its cause instead
(lib/db/errors.ts).
- npm test runs test files one at a time, since database test files share
one database.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Why
Our backend sends each new signup to Harbour with
POST /api/v1/lists/{listId}/contacts. Three contacts have been getting HTTP 500 with an empty body on every hourly retry since 17 Oct, 20 Oct and 5 Nov. The backend logsstatus: 500, body: ""from harbour.autter-logs.com (attempt 960 and counting).The route did a plain
insert(contacts). When the contact was already in the list, the(list_id, email)unique constraint threw, nothing caught it (the route andwithApiAuthboth let it through), and Next.js sent its empty 500. The client counts 2xx and 409 as success and retries anything else, so a duplicate is retried forever.What
onConflictDoNothingon(list_id, email). On a conflict, answer 409 in the usual envelope:{ "data": { "id", "email", "status" }, "meta": {}, "error": "A contact with this email already exists in this list" }.No confirmation or verification job is queued for the existing contact, and no audit entry is written.
logAuditandenqueueContactEmailVerificationalready swallow their own errors today, so this is defensive rather than a live bug.withApiAuth: any error from auth, rate limiting or the handler is logged with method and path and answered with{ data: null, meta: {}, error: "Internal server error" }(status 500). This covers every v1 route. No stack or SQL goes in the body.unique/duplicateinerr.message. drizzle-orm 0.45 wraps driver errors inDrizzleQueryError, whose message is the failed SQL, so the check never matched and dashboard duplicates also became 500s. Newlib/db/errors.tsisUniqueViolation()checks SQLSTATE23505on the error or itscause.npm testnow runs test files one at a time (--test-concurrency=1), since database test files share one database. CONTRIBUTING updated.Other v1 write routes checked
POST /lists/{id}/contacts/bulk: already upserts withonConflictDoUpdate, so it has no duplicate 500. One separate problem: if the same email appears twice in one batch, Postgres rejects the whole batch ("cannot affect row a second time") and the route counts all of it (up to 500 rows) asskipped. Not changed here.POST /suppressionsand/suppressions/bulk: already useonConflictDoNothing.POST /listsand/campaigns: no unique constraints.listIdthat is not a uuid makes Postgres raise22P02. That is now a JSON 500 instead of an empty one, but still a 5xx, so the client would retry it. Not changed here.Tests
New
test/v1-contacts.test.ts(Postgres integration, same pattern astest/email-checker.test.ts):send/insertmocked to throw andaudit_logsrenamed away: still 201, and the contact exists.{ data: null, meta: {}, error: "Internal server error" }.Run locally against Postgres 17:
TEST_DATABASE_URL=... CI=1 npm test: 20/20 pass. Without the fix, the duplicate, catch-all, PUT and internal tests fail (4 of 7). The side-effect test passes without the fix too, which confirms those helpers already swallowed their own errors.npx tsc --noEmit: clean.npm run lint: exit 0, no new warnings.Deploying
Merging does not deploy anything that I could verify:
docker.ymlhas failed on all of its last 50 runs because the tagghcr.io/Autter-dev/hedwig-mailhas uppercase letters, so no image has been pushed.hedwig / productionstop at3c2b813(2026-05-07). None of the later commits on main shows a deployment.Someone has to deploy this by hand.
🤖 Generated with Claude Code