feat: add error handling for duplicated semantic ID and label name
All checks were successful
Frontend CI / build (push) Successful in 1m21s

This commit is contained in:
SquidSpirit 2025-10-12 20:58:21 +08:00
parent 88aceb3a51
commit 9f17f45862
8 changed files with 31 additions and 6 deletions

View File

@ -5,6 +5,8 @@ pub enum PostError {
NotFound,
Unauthorized,
InvalidSemanticId,
DuplicatedSemanticId,
DuplicatedLabelName,
Unexpected(anyhow::Error),
}
@ -17,6 +19,8 @@ impl Display for PostError {
f,
"Semantic ID shouldn't be numeric and must conform to `^[0-9a-zA-Z_\\-]+$`"
),
PostError::DuplicatedSemanticId => write!(f, "Semantic ID already exists"),
PostError::DuplicatedLabelName => write!(f, "Label name already exists"),
PostError::Unexpected(e) => write!(f, "Unexpected error: {}", e),
}
}

View File

@ -34,7 +34,10 @@ pub async fn create_label_handler(
Ok(label) => HttpResponse::Created().json(label),
Err(e) => match e {
PostError::Unauthorized => HttpResponse::Unauthorized().finish(),
PostError::NotFound | PostError::InvalidSemanticId => {
PostError::DuplicatedLabelName => HttpResponse::Conflict().finish(),
PostError::NotFound
| PostError::InvalidSemanticId
| PostError::DuplicatedSemanticId => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}

View File

@ -37,7 +37,8 @@ pub async fn create_post_handler(
Err(e) => match e {
PostError::Unauthorized => HttpResponse::Unauthorized().finish(),
PostError::InvalidSemanticId => HttpResponse::BadRequest().finish(),
PostError::NotFound => {
PostError::DuplicatedSemanticId => HttpResponse::Conflict().finish(),
PostError::NotFound | PostError::DuplicatedLabelName => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}

View File

@ -24,7 +24,11 @@ pub async fn get_all_labels_handler(
match result {
Ok(labels) => HttpResponse::Ok().json(labels),
Err(e) => match e {
PostError::NotFound | PostError::Unauthorized | PostError::InvalidSemanticId => {
PostError::NotFound
| PostError::Unauthorized
| PostError::InvalidSemanticId
| PostError::DuplicatedSemanticId
| PostError::DuplicatedLabelName => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}

View File

@ -36,7 +36,11 @@ pub async fn get_all_post_info_handler(
match result {
Ok(post_info_list) => HttpResponse::Ok().json(post_info_list),
Err(e) => match e {
PostError::NotFound | PostError::Unauthorized | PostError::InvalidSemanticId => {
PostError::NotFound
| PostError::Unauthorized
| PostError::InvalidSemanticId
| PostError::DuplicatedSemanticId
| PostError::DuplicatedLabelName => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}

View File

@ -34,7 +34,9 @@ pub async fn get_post_by_id_handler(
Err(e) => match e {
PostError::NotFound => HttpResponse::NotFound().finish(),
PostError::Unauthorized => HttpResponse::Unauthorized().finish(),
PostError::InvalidSemanticId => {
PostError::InvalidSemanticId
| PostError::DuplicatedSemanticId
| PostError::DuplicatedLabelName => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}

View File

@ -40,7 +40,8 @@ pub async fn update_label_handler(
Err(e) => match e {
PostError::NotFound => HttpResponse::NotFound().finish(),
PostError::Unauthorized => HttpResponse::Unauthorized().finish(),
PostError::InvalidSemanticId => {
PostError::DuplicatedLabelName => HttpResponse::Conflict().finish(),
PostError::InvalidSemanticId | PostError::DuplicatedSemanticId => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}

View File

@ -1,4 +1,5 @@
use actix_web::{HttpResponse, Responder, web};
use anyhow::anyhow;
use auth::framework::web::auth_middleware::UserId;
use sentry::integrations::anyhow::capture_anyhow;
@ -39,7 +40,12 @@ pub async fn update_post_handler(
Err(e) => match e {
PostError::NotFound => HttpResponse::NotFound().finish(),
PostError::Unauthorized => HttpResponse::Unauthorized().finish(),
PostError::DuplicatedSemanticId => HttpResponse::Conflict().finish(),
PostError::InvalidSemanticId => HttpResponse::BadRequest().finish(),
PostError::DuplicatedLabelName => {
capture_anyhow(&anyhow!(e));
HttpResponse::InternalServerError().finish()
}
PostError::Unexpected(e) => {
capture_anyhow(&e);
HttpResponse::InternalServerError().finish()