fix(db): map 23505 unique_violation to DomainError::Conflict (→ HTTP 409); close TOCTOU on register save

This commit is contained in:
2026-05-15 17:03:48 +02:00
parent a02ae3e662
commit 15b1d0fdb7
2 changed files with 88 additions and 4 deletions

View File

@@ -6,6 +6,15 @@ pub(crate) trait IntoDbResult<T> {
impl<T> IntoDbResult<T> for Result<T, sqlx::Error> {
fn into_domain(self) -> Result<T, DomainError> {
self.map_err(|e| DomainError::Internal(e.to_string()))
self.map_err(|e| {
if let sqlx::Error::Database(ref db) = e {
if db.code().as_deref() == Some("23505") {
return DomainError::Conflict(
db.constraint().unwrap_or("conflict").to_string(),
);
}
}
DomainError::Internal(e.to_string())
})
}
}