diff --git a/server-rs/crates/api-server/src/asset_billing.rs b/server-rs/crates/api-server/src/asset_billing.rs index 741665a1e..990bd3cd7 100644 --- a/server-rs/crates/api-server/src/asset_billing.rs +++ b/server-rs/crates/api-server/src/asset_billing.rs @@ -254,17 +254,27 @@ pub(crate) fn map_asset_operation_wallet_error(error: SpacetimeClientError) -> A error = %message, "资产操作泥点预扣失败" ); - let status = match &error { - SpacetimeClientError::Procedure(message) if message.contains("泥点余额不足") => { - StatusCode::CONFLICT - } - _ => StatusCode::BAD_GATEWAY, + let is_insufficient_balance = matches!( + &error, + SpacetimeClientError::Procedure(message) if message.contains("泥点余额不足") + ); + let status = if is_insufficient_balance { + StatusCode::CONFLICT + } else { + StatusCode::BAD_GATEWAY }; + let public_message = is_insufficient_balance.then(|| message.clone()); - AppError::from_status(status).with_details(json!({ + let app_error = AppError::from_status(status).with_details(json!({ "provider": "profile-wallet", "message": message, - })) + })); + + if let Some(public_message) = public_message { + app_error.with_message(public_message) + } else { + app_error + } } pub(crate) fn should_skip_asset_operation_billing_for_connectivity( @@ -315,6 +325,24 @@ mod tests { )); } + #[test] + fn asset_operation_wallet_insufficient_balance_is_public_message() { + let error = map_asset_operation_wallet_error(SpacetimeClientError::Procedure( + "泥点余额不足".to_string(), + )); + + assert_eq!(error.status_code(), StatusCode::CONFLICT); + assert_eq!(error.code(), "CONFLICT"); + assert_eq!(error.message(), "泥点余额不足"); + assert_eq!( + error + .details() + .and_then(|details| details.get("message")) + .and_then(serde_json::Value::as_str), + Some("泥点余额不足"), + ); + } + #[test] fn asset_operation_billing_does_not_refund_stale_worker_lease_errors() { let stale_error = AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({