fix(postgres): map unique constraint violations to DomainError::UniqueViolation
This commit is contained in:
@@ -166,7 +166,18 @@ impl UserWriter for PgUserRepository {
|
|||||||
.bind(user.updated_at)
|
.bind(user.updated_at)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.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(|_| ())
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user