diff --git a/server-rs/crates/api-server/src/error_reports.rs b/server-rs/crates/api-server/src/error_reports.rs index b57359e77..48315f849 100644 --- a/server-rs/crates/api-server/src/error_reports.rs +++ b/server-rs/crates/api-server/src/error_reports.rs @@ -3,6 +3,7 @@ use crate::{ api_response::json_success_body, auth::{AuthenticatedAccessToken, require_bearer_auth}, http_error::AppError, + platform_errors, request_context::RequestContext, state::AppState, tracking::{TrackingEventDraft, record_tracking_event_after_success}, @@ -273,7 +274,7 @@ pub async fn admin_get_error_report( .spacetime_client() .get_error_report(batch_id.clone()) .await - .map_err(|_| AppError::from_status(StatusCode::NOT_FOUND).with_message("报告不存在"))?; + .map_err(map_error_report_spacetime_error)?; let bytes = state .oss_client() .ok_or_else(|| internal("OSS 未配置"))? @@ -285,7 +286,7 @@ pub async fn admin_get_error_report( }, ) .await - .map_err(|_| AppError::from_status(StatusCode::NOT_FOUND).with_message("报告附件不存在"))?; + .map_err(map_error_report_oss_error)?; let (events, desc, logs) = parse_archive(&bytes).map_err(|e| internal(format!("报告归档损坏:{e}")))?; record_admin_report_audit( @@ -389,7 +390,7 @@ pub async fn admin_download_error_report( .spacetime_client() .get_error_report(batch_id.clone()) .await - .map_err(|_| AppError::from_status(StatusCode::NOT_FOUND).with_message("报告不存在"))?; + .map_err(map_error_report_spacetime_error)?; let bytes = state .oss_client() .ok_or_else(|| internal("OSS 未配置"))? @@ -401,7 +402,7 @@ pub async fn admin_download_error_report( }, ) .await - .map_err(|_| AppError::from_status(StatusCode::NOT_FOUND).with_message("报告附件不存在"))?; + .map_err(map_error_report_oss_error)?; record_admin_report_audit( &state, &ctx, @@ -429,6 +430,26 @@ fn bad_request(m: impl Into) -> AppError { fn internal(e: E) -> AppError { AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_message(e.to_string()) } + +fn map_error_report_oss_error(error: platform_oss::OssError) -> AppError { + match error.kind() { + platform_oss::OssErrorKind::ObjectNotFound => { + AppError::from_status(StatusCode::NOT_FOUND).with_message("报告附件不存在") + } + platform_oss::OssErrorKind::InvalidRequest => { + AppError::from_status(StatusCode::BAD_REQUEST).with_message("报告附件读取请求无效") + } + _ => platform_errors::map_oss_error(error, "error-report"), + } +} + +fn map_error_report_spacetime_error(error: spacetime_client::SpacetimeClientError) -> AppError { + if error.to_string() == "错误报告不存在" { + AppError::from_status(StatusCode::NOT_FOUND).with_message("报告不存在") + } else { + internal(error) + } +} async fn record_admin_report_audit( state: &AppState, ctx: &RequestContext,