feat: add error handling for duplicated semantic ID and label name in database operations
This commit is contained in:
parent
9f17f45862
commit
78dfca41e6
@ -32,7 +32,14 @@ impl LabelDbService for LabelDbServiceImpl {
|
||||
)
|
||||
.fetch_one(&self.db_pool)
|
||||
.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)
|
||||
}
|
||||
@ -50,7 +57,14 @@ impl LabelDbService for LabelDbServiceImpl {
|
||||
)
|
||||
.execute(&self.db_pool)
|
||||
.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();
|
||||
|
||||
if affected_rows == 0 {
|
||||
|
@ -211,7 +211,14 @@ impl PostDbService for PostDbServiceImpl {
|
||||
)
|
||||
.fetch_one(&mut *tx)
|
||||
.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() {
|
||||
sqlx::query!(
|
||||
@ -266,7 +273,14 @@ impl PostDbService for PostDbServiceImpl {
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.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();
|
||||
|
||||
if affected_rows == 0 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user