Skip to content

fix(convex): throw ConvexError codes from org/user lifecycle mutations (#2018) - #2213

Merged
cursor[bot] merged 3 commits into
mainfrom
tale/xs7epgw2w66hzc48c08qe44kp589fn6g
Jul 3, 2026
Merged

cursor[bot] merged 3 commits into
mainfrom
tale/xs7epgw2w66hzc48c08qe44kp589fn6g

Conversation

@larryro

@larryro larryro commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Resolves #2018. organizations/delete_cleanup.ts, users/create_member.ts and users/create_user_without_session.ts threw raw Error, so every rejection surfaced as an opaque "Server Error". They now throw ConvexError({ code }) with stable codes the UI can branch on.

Codes introduced

Location Condition Code
delete_cleanup unauthenticated UNAUTHENTICATED
delete_cleanup non-owner delete FORBIDDEN
delete_cleanup delete default org DEFAULT_ORG_PROTECTED
create_member unauthenticated UNAUTHENTICATED
create_member non-admin caller FORBIDDEN
create_member already a member DUPLICATE_MEMBER
create_member / create_user_without_session signup failed USER_CREATION_FAILED
create_user_without_session email already exists DUPLICATE_EMAIL

Frontend

The add-member dialog now surfaces DUPLICATE_MEMBER as an inline error on the email field (new auth.validation.emailAlreadyMember key in en/de/fr), matching the existing PASSWORD_REQUIRED inline pattern — these are validation errors users should fix in-form, per the issue notes.

Tests

  • convex/organizations/delete_cleanup.test.ts — new: asserts each rejection throws ConvexError with the expected code + the success path.
  • member-add-dialog.test.tsx — added a DUPLICATE_MEMBER regression test.
  • i18n parity, typecheck, oxlint and oxfmt all green.

Closes #2018

#2018)

delete_cleanup.ts, create_member.ts and create_user_without_session.ts threw raw
Error, surfacing every rejection as an opaque "Server Error". They now throw
ConvexError with stable codes (UNAUTHENTICATED, FORBIDDEN, DEFAULT_ORG_PROTECTED,
DUPLICATE_MEMBER, DUPLICATE_EMAIL, USER_CREATION_FAILED).

The add-member dialog now shows DUPLICATE_MEMBER inline on the email field
(emailAlreadyMember in en/de/fr). Adds unit tests for the org-deletion codes and
a dialog regression test.

Closes #2018
@larryro

larryro commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

Desk review — #2018 (ConvexError codes for org/user lifecycle mutations)

CI: all required checks green (Type check, Unit, UI, Lint, Format, Build platform/convex, Playwright ×16, Smoke — pass; the skipping entries are conditional fork/scheduled jobs). Tests run locally on this branch: bunx vitest --run --project server convex/organizations/delete_cleanup.test.ts → 5 passed; bunx vitest --run --project client app/features/settings/organization/components/member-add-dialog.test.tsx → 4 passed.

What's solid

  • All throw-sites named in Bug: organizations/delete_cleanup.ts + users/create_member.ts throw raw Error — org/user lifecycle mutations are opaque #2018 are converted to ConvexError({ code, message }) with sensible codes; ConvexError is imported in every file that uses it; no stray throw new Error remains in the three backend files.
  • The DUPLICATE_MEMBER inline-error handling in member-add-dialog.tsx cleanly mirrors the existing PASSWORD_REQUIRED pattern (reads error.data?.code, sets a field error, early-returns before the generic toast). No ordering/shadowing issue.
  • i18n: auth.validation.emailAlreadyMember added to en/de/fr. Not adding it to de-CH.json is correct — that file is a sparse override (its auth.validation only overrides passwordUppercase) and falls back to de.json.
  • delete_cleanup.test.ts is thorough: all three error branches + success path + "never a raw Error", asserting on stable data.code.

Blocking

1. No backend test for create_member.ts's converted error branches. create_member.ts is a primary target of #2018 and now emits UNAUTHENTICATED, FORBIDDEN, DUPLICATE_MEMBER, and USER_CREATION_FAILED (×2). Nothing verifies the backend actually throws these codes: the new dialog test mocks createMember and fabricates a DUPLICATE_MEMBER ConvexError, so the backend→frontend contract for the very code the new inline feature depends on is unverified at the emitting end. The repo has a clear convention for this — convex/users/update_user_password.test.ts and the new convex/organizations/delete_cleanup.test.ts both assert rejects.toMatchObject({ data: { code: ... } }). Please add convex/users/create_member.test.ts covering at least DUPLICATE_MEMBER, and ideally UNAUTHENTICATED / FORBIDDEN / USER_CREATION_FAILED, following that same pattern.

Non-blocking notes (not required for merge)

  • create_user_without_session.ts's new DUPLICATE_EMAIL / USER_CREATION_FAILED codes have no caller anywhere in the repo (the function is exported but unused) and no test — the conversion is in-scope and harmless, just currently unreachable. The issue's "A user with this email already exists … display inline" note is effectively satisfied by DUPLICATE_MEMBER in the add-member flow, since create_member re-adds an existing user to a new org rather than throwing on a duplicate email.
  • use-delete-organization.ts still renders the generic Convex stacktrace in its failure toast (it reads err.message, not err.data). This is not a regression — raw Errors were already scrubbed to an opaque "Server Error" before this PR — and not required by the issue, but consuming the new DEFAULT_ORG_PROTECTED / FORBIDDEN codes there would complete the de-opaque intent.

Verdict: NOT READY — CHANGES REQUIRED (add backend test coverage for create_member.ts's ConvexError codes, per item 1).

Adds convex/users/create_member.test.ts asserting the converted
lifecycle rejections throw ConvexError with stable codes
(UNAUTHENTICATED, FORBIDDEN, DUPLICATE_MEMBER, PASSWORD_REQUIRED,
USER_CREATION_FAILED), per desk-review feedback on #2213.

Follows the rejects.toMatchObject({ data: { code } }) pattern from
update_user_password.test.ts and delete_cleanup.test.ts.

@larryro larryro left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Desk Review — #2018 (ConvexError codes for org/user lifecycle mutations)

Verdict: READY TO MERGE.

Verification performed

  • Checked out branch tale/xs7epgw2w66hzc48c08qe44kp589fn6g, installed deps with bun install.
  • Ran the project's own tests:
    • bunx vitest --run --project server convex/organizations/delete_cleanup.test.ts convex/users/create_member.test.ts → 2 files, 13 tests passed.
    • bunx vitest --run --project client member-add-dialog → 1 file, 4 tests passed.
  • CI on this PR: all 45 checks green (gh pr checks), including Type check, Lint, Format, Knip, Unit, Browser, UI, and 16 Playwright shards. (The skipping entries are fork-PR-only jobs, not failures.)

What's correct

  • All eight in-scope raw throw new Error(...) sites from the issue are now ConvexError({ code, message }) with sensible, stable codes — grep "throw new Error" returns zero matches across the three files. Codes: UNAUTHENTICATED, FORBIDDEN, DEFAULT_ORG_PROTECTED, DUPLICATE_MEMBER, USER_CREATION_FAILED, DUPLICATE_EMAIL.
  • Control flow / check ordering unchanged (auth → role → slug/membership); success return values unchanged. The DUPLICATE_MEMBER check is correctly gated behind the admin authorization check, so it can't be used for unauthenticated user-enumeration.
  • DUPLICATE_MEMBER is wired to an inline email-field error in member-add-dialog.tsx, mirroring the existing PASSWORD_REQUIRED (#1470) pattern exactly. New i18n key auth.validation.emailAlreadyMember is present and genuinely translated in en/de/fr.
  • New tests assert error.data.code for every reachable branch and that rejections are instanceof ConvexError (regression guard against raw Error).

Non-blocking observations (not required for merge)

  1. app/features/organization/hooks/use-delete-organization.ts (~line 86): the catch shows err.message in the toast description. For a ConvexError, .message is the JSON-serialized payload ({"code":"FORBIDDEN","message":"..."}), not a clean string. Pre-fix this flow already surfaced a masked "Server Error", so this is not a regression in capability, but consider reading err.data?.message/err.data?.code for a cleaner toast. The issue only required inline display for the two validation codes, which is done.
  2. convex/users/create_user_without_session.ts is a plain helper with no in-repo caller (not wrapped in mutation()), so its new DUPLICATE_EMAIL code has no UI to wire to and its three error branches are untested. This is pre-existing orphaned code; converting its errors is exactly what the issue asked, so it's acceptable scope — flagging only for awareness.
  3. The dialog test asserts the error via findByText rather than binding to the email field specifically — passes and is adequate, just slightly less strict than ideal.

None of the above block the merge. The change is focused, follows repo conventions, fixes the reported issue, and is fully covered by passing tests and green CI.

READY TO MERGE.

@cursor
cursor Bot merged commit b5dab04 into main Jul 3, 2026
51 checks passed
@cursor
cursor Bot deleted the tale/xs7epgw2w66hzc48c08qe44kp589fn6g branch July 3, 2026 13:29
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.

Bug: organizations/delete_cleanup.ts + users/create_member.ts throw raw Error — org/user lifecycle mutations are opaque

1 participant