Skip to content

fix: return 409 Conflict instead of 500 when creating a duplicate admin or realm - #80

Merged
pointche-ens merged 2 commits into
developfrom
fix/admin-realm-create-conflict
Sep 4, 2026
Merged

fix: return 409 Conflict instead of 500 when creating a duplicate admin or realm#80
pointche-ens merged 2 commits into
developfrom
fix/admin-realm-create-conflict

Conversation

@pointche-ens

Copy link
Copy Markdown
Contributor

Summary

  • POST /admins and POST /admins/realms used to leak a raw 500 with database internals when the target ID already existed (e.g. re-creating the seeded admin user, or an existing realm), instead of a clean 409 Conflict.
  • This mirrors the fix already applied to POST /realms/{realm_id}/userpass (AuthDbError::from_insert_error), which hadn't been extended to the admin/realm creation paths.
  • Applied to all three database backends (PostgreSQL, MySQL, SQLite).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The core behavior change is correctly implemented across all backends and covered by targeted tests, with only a minor test naming/doc wording nit noted.

Pull request overview

This PR updates the database insert paths for realm and admin creation so that attempting to create an already-existing realm/admin returns a clean 409 Conflict (instead of bubbling up a 500 with database error internals), aligning behavior across SQLite/PostgreSQL/MySQL and strengthening the API’s error contract.

Changes:

  • Map unique-constraint insert failures to AuthDbError::Conflict for create_realm and create_admin across SQLite/PostgreSQL/MySQL.
  • Add/adjust integration tests to assert duplicate realm/admin creation yields 409.
  • Add a branch changelog entry documenting the behavior change.
File summaries
File Description
server/src/tests/super_admin_api.rs Adds a test ensuring duplicate realm creation returns 409 Conflict.
server/src/tests/admin_api.rs Tightens the duplicate admin creation test to explicitly assert 409 (and updates imports).
server/src/database/impls/sqlite.rs Maps duplicate realm/admin insert errors to Conflict via AuthDbError::from_insert_error.
server/src/database/impls/postgres.rs Same as SQLite for PostgreSQL backend.
server/src/database/impls/mysql.rs Same as SQLite for MySQL backend.
CHANGELOG/fix_admin-realm-create-conflict.md Documents the 500→409 behavior change for duplicate admin/realm creation.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread server/src/tests/admin_api.rs Outdated
@pointche-ens
pointche-ens merged commit 77fbb29 into develop Sep 4, 2026
61 checks passed
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.

3 participants