区分错误报告读取失败状态

仅将明确不存在的 OSS 或报告映射为 404

将请求无效、上游和 SpacetimeDB 故障映射为对应错误状态
This commit is contained in:
2026-09-02 19:03:08 +08:00
parent 7cf6a4c683
commit 733939269f
@@ -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<String>) -> AppError {
fn internal<E: ToString>(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,