From 883bcb182db240a9eb1eb75aa0b2ebccdc24555e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E5=AE=87?= Date: Fri, 2 Oct 2026 20:17:00 +0800 Subject: [PATCH] =?UTF-8?q?HTTP=20=E9=94=99=E8=AF=AF=E5=85=9C=E5=BA=95?= =?UTF-8?q?=E4=BC=98=E5=85=88=E6=9C=8D=E5=8A=A1=E7=AB=AF=20code=EF=BC=8C?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B8=8D=E5=86=8D=E6=8B=BC=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=90=8D=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 兜底 --- .../src-tauri/src/account_api.rs | 23 +++++++++++---- .../src/game_distribution_publish.rs | 12 +++++--- .../src/game_package_upload/runtime.rs | 28 +++++++++++++------ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/apps/ai-game-creator-shell/src-tauri/src/account_api.rs b/apps/ai-game-creator-shell/src-tauri/src/account_api.rs index 2cdc1b40d..a8a05391d 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/account_api.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/account_api.rs @@ -78,6 +78,17 @@ fn error_message(body: &str) -> Option { .map(ToString::to_string) } +fn error_code(body: &str) -> Option { + let value = serde_json::from_str::(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 { 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)) } diff --git a/apps/ai-game-creator-shell/src-tauri/src/game_distribution_publish.rs b/apps/ai-game-creator-shell/src-tauri/src/game_distribution_publish.rs index 35acdde86..c3b2aabf9 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/game_distribution_publish.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/game_distribution_publish.rs @@ -193,8 +193,11 @@ fn response_data(body: &str) -> Result { 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); diff --git a/apps/ai-game-creator-shell/src-tauri/src/game_package_upload/runtime.rs b/apps/ai-game-creator-shell/src-tauri/src/game_package_upload/runtime.rs index 259757a5e..cc76558c1 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/game_package_upload/runtime.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/game_package_upload/runtime.rs @@ -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}"))?;