From 9f17f45862e3023e5e363b4ae41e7f3df993bfe5 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Sun, 12 Oct 2025 20:58:21 +0800 Subject: [PATCH] feat: add error handling for duplicated semantic ID and label name --- backend/feature/post/src/application/error/post_error.rs | 4 ++++ .../feature/post/src/framework/web/create_label_handler.rs | 5 ++++- .../feature/post/src/framework/web/create_post_handler.rs | 3 ++- .../post/src/framework/web/get_all_labels_handler.rs | 6 +++++- .../post/src/framework/web/get_all_post_info_handler.rs | 6 +++++- .../post/src/framework/web/get_post_by_id_handler.rs | 4 +++- .../feature/post/src/framework/web/update_label_handler.rs | 3 ++- .../feature/post/src/framework/web/update_post_handler.rs | 6 ++++++ 8 files changed, 31 insertions(+), 6 deletions(-) diff --git a/backend/feature/post/src/application/error/post_error.rs b/backend/feature/post/src/application/error/post_error.rs index 4d13e4e..275efcc 100644 --- a/backend/feature/post/src/application/error/post_error.rs +++ b/backend/feature/post/src/application/error/post_error.rs @@ -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), } } diff --git a/backend/feature/post/src/framework/web/create_label_handler.rs b/backend/feature/post/src/framework/web/create_label_handler.rs index a06f2c2..ca1fda6 100644 --- a/backend/feature/post/src/framework/web/create_label_handler.rs +++ b/backend/feature/post/src/framework/web/create_label_handler.rs @@ -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() } diff --git a/backend/feature/post/src/framework/web/create_post_handler.rs b/backend/feature/post/src/framework/web/create_post_handler.rs index f8ee1b3..e884022 100644 --- a/backend/feature/post/src/framework/web/create_post_handler.rs +++ b/backend/feature/post/src/framework/web/create_post_handler.rs @@ -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() } diff --git a/backend/feature/post/src/framework/web/get_all_labels_handler.rs b/backend/feature/post/src/framework/web/get_all_labels_handler.rs index 2aac104..7ad0cda 100644 --- a/backend/feature/post/src/framework/web/get_all_labels_handler.rs +++ b/backend/feature/post/src/framework/web/get_all_labels_handler.rs @@ -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() } diff --git a/backend/feature/post/src/framework/web/get_all_post_info_handler.rs b/backend/feature/post/src/framework/web/get_all_post_info_handler.rs index a18e412..f3f084d 100644 --- a/backend/feature/post/src/framework/web/get_all_post_info_handler.rs +++ b/backend/feature/post/src/framework/web/get_all_post_info_handler.rs @@ -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() } diff --git a/backend/feature/post/src/framework/web/get_post_by_id_handler.rs b/backend/feature/post/src/framework/web/get_post_by_id_handler.rs index 891265d..9705eb2 100644 --- a/backend/feature/post/src/framework/web/get_post_by_id_handler.rs +++ b/backend/feature/post/src/framework/web/get_post_by_id_handler.rs @@ -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() } diff --git a/backend/feature/post/src/framework/web/update_label_handler.rs b/backend/feature/post/src/framework/web/update_label_handler.rs index cb06770..f72352f 100644 --- a/backend/feature/post/src/framework/web/update_label_handler.rs +++ b/backend/feature/post/src/framework/web/update_label_handler.rs @@ -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() } diff --git a/backend/feature/post/src/framework/web/update_post_handler.rs b/backend/feature/post/src/framework/web/update_post_handler.rs index 40cce3e..ef256b3 100644 --- a/backend/feature/post/src/framework/web/update_post_handler.rs +++ b/backend/feature/post/src/framework/web/update_post_handler.rs @@ -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()