fix(postgres): map unique constraint violations to DomainError::UniqueViolation

This commit is contained in:
2026-05-16 10:57:04 +02:00
parent ca35e8e774
commit 1ab3766ce8

View File

@@ -166,7 +166,18 @@ impl UserWriter for PgUserRepository {
.bind(user.updated_at)
.execute(&self.pool)
.await
.into_domain()
.map_err(|e| {
if let sqlx::Error::Database(ref db) = e {
if db.code().as_deref() == Some("23505") {
return match db.constraint().unwrap_or("") {
"users_username_key" => DomainError::UniqueViolation { field: "username" },
"users_email_key" => DomainError::UniqueViolation { field: "email" },
_ => DomainError::UniqueViolation { field: "unknown" },
};
}
}
DomainError::Internal(e.to_string())
})
.map(|_| ())
}