fix: handle registration commit errors with proper JSON response#224
fix: handle registration commit errors with proper JSON response#224jigangz wants to merge 1 commit intodataelement:mainfrom
Conversation
…aelement#223) When registering the second user (non-admin), the register endpoint relied on get_db()'s implicit commit after the function returned. If that deferred commit hit a database constraint violation (IntegrityError), FastAPI returned a raw 500 response with no JSON body. The frontend parsed this as 'Request failed' with no useful error message. Changes: - Add explicit db.commit() for all registration paths (not just first user) - Catch IntegrityError and return a proper 409 JSON response - Remove redundant intermediate db.flush() calls — the single commit handles both User and Participant creation atomically - First-user path: commit now happens before seed_default_agents() as before, just unified with the non-first-user path
|
Thank you for the detailed investigation and the well-written PR, @jigangz! We really appreciate you digging into the root cause and putting together a clean fix. After reviewing the current codebase, the legacy Given that the scenario is difficult to hit in practice and the underlying registration flow has been reworked, we are going to close this PR for now. That said, your analysis is valid and the defensive pattern you proposed (wrapping the commit in a try/except IntegrityError) is good practice — we will keep it in mind for a future hardening pass. Thanks again for contributing! |
Summary
Fix "Request failed" error when registering the second user account.
Root cause
The
/auth/registerendpoint only calleddb.commit()explicitly for the first user (platform admin). For subsequent registrations, the commit was deferred to theget_db()dependency's finally block. If that deferred commit hit anIntegrityError(or any other database error), FastAPI returned a raw 500 response with no JSON body — which the frontend displayed as a generic "Request failed" message.Fix
db.commit()for all registration pathsIntegrityErrorand return a proper HTTP 409 JSON response with a descriptive error messagedb.flush()calls — the single commit handles both User and Participant creation atomicallyChanges
backend/app/api/auth.py: unified commit + IntegrityError handlingChecklist
Fixes #228