feat: add error handling for duplicated semantic ID and label name in database operations
All checks were successful
Frontend CI / build (push) Successful in 1m22s
Auto Comment On PR / add_improve_comment (pull_request) Successful in 17s
PR Title Check / pr-title-check (pull_request) Successful in 17s

This commit is contained in:
SquidSpirit 2025-10-12 21:05:51 +08:00
parent 9f17f45862
commit 78dfca41e6
2 changed files with 32 additions and 4 deletions

View File

@ -32,7 +32,14 @@ impl LabelDbService for LabelDbServiceImpl {
) )
.fetch_one(&self.db_pool) .fetch_one(&self.db_pool)
.await .await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))?; .map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_label_name") {
return PostError::DuplicatedLabelName;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?;
Ok(id) Ok(id)
} }
@ -50,7 +57,14 @@ impl LabelDbService for LabelDbServiceImpl {
) )
.execute(&self.db_pool) .execute(&self.db_pool)
.await .await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))? .map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_label_name") {
return PostError::DuplicatedLabelName;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?
.rows_affected(); .rows_affected();
if affected_rows == 0 { if affected_rows == 0 {

View File

@ -211,7 +211,14 @@ impl PostDbService for PostDbServiceImpl {
) )
.fetch_one(&mut *tx) .fetch_one(&mut *tx)
.await .await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))?; .map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_post_semantic_id") {
return PostError::DuplicatedSemanticId;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?;
for (order, &label_id) in label_ids.iter().enumerate() { for (order, &label_id) in label_ids.iter().enumerate() {
sqlx::query!( sqlx::query!(
@ -266,7 +273,14 @@ impl PostDbService for PostDbServiceImpl {
) )
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.map_err(|e| PostError::Unexpected(DatabaseError(e).into()))? .map_err(|e| {
if let sqlx::Error::Database(db_err) = &e {
if db_err.constraint() == Some("idx_post_semantic_id") {
return PostError::DuplicatedSemanticId;
}
}
PostError::Unexpected(DatabaseError(e).into())
})?
.rows_affected(); .rows_affected();
if affected_rows == 0 { if affected_rows == 0 {