move db error classification to From<>

This commit is contained in:
Brandon Thomas committed 2024-08-09 01:54:41 -04:00
1 parent d701f0a437
commit 268c7cd8a1
2 files changed
+15 -19

No files matched your search

@@ -22,7 +22,19 @@ impl From<anyhow::Error> for DatabaseQueryError {
impl From<sqlx::Error> for DatabaseQueryError {
fn from(err: sqlx::Error) -> Self {
DatabaseQueryError::SqlxError(err)
if let Some(db_err) = err.as_database_error() {
// NB: SQLSTATE[23000]: Integrity constraint violation
// NB: MySQL Error Code 1062: Duplicate key insertion (this is harder to access)
let is_integrity_violation = db_err.code().as_deref() == Some("23000");
let is_duplicate_key = db_err.message().contains("Duplicate entry");
let is_idempotency_error = db_err.message().contains("uuid_idempotency_token");
if is_integrity_violation && is_duplicate_key && is_idempotency_error {
return Self::IdempotencyDuplicateKeyError;
}
}
Self::SqlxError(err)
}
}
@@ -156,24 +156,8 @@ SET
.await;
let record_id = match query_result {
Ok(res) => {
res.last_insert_id()
},
Err(err) => {
if let Some(db_err) = err.as_database_error() {
// NB: SQLSTATE[23000]: Integrity constraint violation
// NB: MySQL Error Code 1062: Duplicate key insertion (this is harder to access)
let is_integrity_violation = db_err.code().as_deref() == Some("23000");
let is_duplicate_key = db_err.message().contains("Duplicate entry");
let is_idempotency_error = db_err.message().contains("uuid_idempotency_token");
if is_integrity_violation && is_duplicate_key && is_idempotency_error {
return Err(DatabaseQueryError::IdempotencyDuplicateKeyError);
}
}
return Err(DatabaseQueryError::SqlxError(err));
}
Err(err) => return Err(DatabaseQueryError::from(err)),
Ok(res) => res.last_insert_id(),
};
Ok((job_token, record_id))