fix(db): map 23505 unique_violation to DomainError::Conflict (→ HTTP 409); close TOCTOU on register save
This commit is contained in:
@@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user