refactor(application): use UniqueViolation in register — remove postgres constraint strings

This commit is contained in:
2026-05-16 10:58:14 +02:00
parent 1ab3766ce8
commit 8628acfb77

View File

@@ -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,