HTTP 错误兜底优先服务端 code,叶子不再拼操作名前缀

game_package_upload 三处不再丢弃服务端 code,改成 message → code → HTTP {status}
去掉「读取上传状态失败(HTTP 503)」这类叶子里的操作名前缀,操作名交给调用方话术
game_distribution_publish response_data 用上被丢掉的 error.code
game_distribution_publish 封面任务终态兜底统一成「服务器未返回错误信息」
account_api envelope 分支补 error.code 兜底
This commit is contained in:
2026-10-02 20:17:00 +08:00
parent 14afd21d7e
commit 883bcb182d
3 changed files with 46 additions and 17 deletions
@@ -78,6 +78,17 @@ fn error_message(body: &str) -> Option<String> {
.map(ToString::to_string)
}
fn error_code(body: &str) -> Option<String> {
let value = serde_json::from_str::<Value>(body).ok()?;
let error = value.get("error").unwrap_or(&value);
error
.get("code")
.and_then(Value::as_str)
.map(str::trim)
.filter(|code| !code.is_empty())
.map(ToString::to_string)
}
fn map_http_error(status: StatusCode, body: &str, fallback: &str) -> String {
crate::platform_maintenance::watch_platform_response(status.as_u16(), body);
if status == StatusCode::UNAUTHORIZED {
@@ -89,7 +100,9 @@ fn map_http_error(status: StatusCode, body: &str, fallback: &str) -> String {
error_message(body).unwrap_or_else(|| "当前账号无权执行此操作".to_string())
);
}
let detail = error_message(body).unwrap_or_else(|| format!("HTTP {}", status.as_u16()));
let detail = error_message(body)
.or_else(|| error_code(body))
.unwrap_or_else(|| format!("HTTP {}", status.as_u16()));
format!("{fallback}:{detail}")
}
@@ -97,10 +110,10 @@ fn unwrap_envelope(body: &str, fallback: &str) -> Result<Value, String> {
let value: Value =
serde_json::from_str(body).map_err(|_| format!("{fallback}:服务端响应不是合法 JSON"))?;
if value.get("ok").and_then(Value::as_bool) == Some(false) {
return Err(format!(
"{fallback}:{}",
error_message(body).unwrap_or_else(|| "服务端请求失败".to_string())
));
let detail = error_message(body)
.or_else(|| error_code(body))
.unwrap_or_else(|| "服务器未返回错误信息".to_string());
return Err(format!("{fallback}:{detail}"));
}
Ok(value.get("data").cloned().unwrap_or(value))
}
@@ -193,8 +193,11 @@ fn response_data(body: &str) -> Result<Value, String> {
let value: Value =
serde_json::from_str(body).map_err(|error| format!("服务端响应不是合法 JSON:{error}"))?;
if value.get("ok").and_then(Value::as_bool) == Some(false) {
let (_, message) = parse_error_payload(body);
return Err(message.unwrap_or_else(|| "服务端请求失败".to_string()));
let (code, message) = parse_error_payload(body);
return Err(message
.or(code)
.filter(|detail| !detail.trim().is_empty())
.unwrap_or_else(|| "服务器未返回错误信息".to_string()));
}
Ok(value.get("data").cloned().unwrap_or(value))
}
@@ -309,7 +312,7 @@ async fn resolve_cover_queue(
.error
.clone()
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| "生成游戏封面失败".to_string()));
.unwrap_or_else(|| "服务器未返回错误信息".to_string()));
}
return Ok(initial);
}
@@ -341,7 +344,8 @@ async fn resolve_cover_queue(
if status.status == "failed" {
return Err(status
.error
.unwrap_or_else(|| "生成游戏封面失败".to_string()));
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| "服务器未返回错误信息".to_string()));
}
if status.status == "completed" {
let result = status.result.unwrap_or(Value::Null);
@@ -88,8 +88,11 @@ async fn read_upload_state(
.await
.map_err(|error| format!("读取上传状态失败:{error}"))?;
if status >= 400 {
let (_, message) = parse_server_error(status, &body);
return Err(message.unwrap_or_else(|| format!("读取上传状态失败(HTTP {status})")));
let (code, message) = parse_server_error(status, &body);
return Err(message
.or(code)
.filter(|detail| !detail.trim().is_empty())
.unwrap_or_else(|| format!("HTTP {status}")));
}
let parsed: Value = serde_json::from_str(&body)
.map_err(|error| format!("上传状态响应不是合法 JSON:{error}"))?;
@@ -139,15 +142,21 @@ async fn upload_chunk(
));
}
if status >= 500 || status == 408 || status == 429 {
let (_, message) = parse_server_error(status, &text);
let (code, message) = parse_server_error(status, &text);
return Err(ChunkUploadError::Retryable(
message.unwrap_or_else(|| format!("发行包分片上传失败(HTTP {status})")),
message
.or(code)
.filter(|detail| !detail.trim().is_empty())
.unwrap_or_else(|| format!("HTTP {status}")),
));
}
if status >= 400 {
let (_, message) = parse_server_error(status, &text);
let (code, message) = parse_server_error(status, &text);
return Err(ChunkUploadError::Fatal(
message.unwrap_or_else(|| format!("发行包分片被拒绝(HTTP {status})")),
message
.or(code)
.filter(|detail| !detail.trim().is_empty())
.unwrap_or_else(|| format!("HTTP {status}")),
));
}
let parsed: Value = serde_json::from_str(&text)
@@ -187,8 +196,11 @@ async fn complete_upload(
.await
.map_err(|error| format!("读取完成响应失败:{error}"))?;
if status >= 400 {
let (_, message) = parse_server_error(status, &text);
return Err(message.unwrap_or_else(|| format!("完成发行包上传失败(HTTP {status})")));
let (code, message) = parse_server_error(status, &text);
return Err(message
.or(code)
.filter(|detail| !detail.trim().is_empty())
.unwrap_or_else(|| format!("HTTP {status}")));
}
let parsed: Value =
serde_json::from_str(&text).map_err(|error| format!("完成响应不是合法 JSON:{error}"))?;