From 8628acfb775ce711749b721a0ce20e231b079d23 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Sat, 16 May 2026 10:58:14 +0200 Subject: [PATCH] =?UTF-8?q?refactor(application):=20use=20UniqueViolation?= =?UTF-8?q?=20in=20register=20=E2=80=94=20remove=20postgres=20constraint?= =?UTF-8?q?=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/application/src/use_cases/auth.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/application/src/use_cases/auth.rs b/crates/application/src/use_cases/auth.rs index b717733..d59001c 100644 --- a/crates/application/src/use_cases/auth.rs +++ b/crates/application/src/use_cases/auth.rs @@ -38,11 +38,15 @@ pub async fn register( .save(&user) .await .map_err(|e| match e { - DomainError::Conflict(c) => match c.as_str() { - "users_username_key" => DomainError::Conflict("username taken".into()), - "users_email_key" => DomainError::Conflict("email taken".into()), - _ => DomainError::Conflict("already exists".into()), - }, + DomainError::UniqueViolation { field: "username" } => { + DomainError::Conflict("username taken".into()) + } + DomainError::UniqueViolation { field: "email" } => { + DomainError::Conflict("email taken".into()) + } + DomainError::UniqueViolation { .. } => { + DomainError::Conflict("already exists".into()) + } other => other, })?; events @@ -136,7 +140,7 @@ mod tests { #[async_trait] impl UserWriter for ConflictOnSaveStore { async fn save(&self, _user: &User) -> Result<(), DomainError> { - Err(DomainError::Conflict("users_username_key".into())) + Err(DomainError::UniqueViolation { field: "username" }) } async fn update_profile( &self, @@ -178,7 +182,7 @@ mod tests { #[async_trait] impl UserWriter for EmailConflictOnSaveStore { async fn save(&self, _user: &User) -> Result<(), DomainError> { - Err(DomainError::Conflict("users_email_key".into())) + Err(DomainError::UniqueViolation { field: "email" }) } async fn update_profile( &self,