From 9261b8ce564d5320678c9440076e207fa407ff09 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Thu, 24 Sep 2026 19:33:57 +0800 Subject: [PATCH] feat(api): the last English reasons on the wire are coded - KeySyncFailed.error: the adoption plan/apply error's own coded message - PricingStatus.error: the refresh error's coded message (the same one the refresh call returned) - TurnView.error: read with its code and arguments, like HistoryRow - the ChatGPT account endpoints' "could not be reached" keeps the data plane's coded reason and adds the context with Msg::in_context, so control.chatgpt_backend_unreachable (a {detail} wrapper) is gone; control.chatgpt_backend_status already carries the backend's body as a passthrough argument The rest of the text-like strings in tw-api are user text, upstream text or rule explanations looked up by rule id. CONTROL_API_VERSION is 16. Co-Authored-By: Claude Opus 5.5 --- crates/tw-api/msg-codes.txt | 1 - crates/tw-api/src/lib.rs | 11 +++++++---- crates/tw-control/src/chatgpt.rs | 8 +++++--- crates/tw-control/src/keys.rs | 2 +- crates/tw-control/src/pricing.rs | 4 ++-- crates/tw-control/tests/pricing.rs | 7 ++++++- crates/tw-store/src/db.rs | 7 ++++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/crates/tw-api/msg-codes.txt b/crates/tw-api/msg-codes.txt index 32875b99..78076121 100644 --- a/crates/tw-api/msg-codes.txt +++ b/crates/tw-api/msg-codes.txt @@ -164,7 +164,6 @@ control.builtin_group_fixed control.callback_ports_busy control.chatgpt_backend_not_json control.chatgpt_backend_status -control.chatgpt_backend_unreachable control.chatgpt_login.approval_unreadable control.chatgpt_login.device_status control.chatgpt_login.expired diff --git a/crates/tw-api/src/lib.rs b/crates/tw-api/src/lib.rs index 1707de0a..6fccacb2 100644 --- a/crates/tw-api/src/lib.rs +++ b/crates/tw-api/src/lib.rs @@ -479,7 +479,10 @@ pub const MSG_CODES: &str = include_str!("../msg-codes.txt"); /// 状态、配置来源、试算的结论和明细……);换了类型的原因:`CredentialRotated` / /// `CredentialExpired` 的 `detail`、`OAuthView.failure`、三处登录的 `error`、 /// `ConfigRejected.message`、`RuleTrace.error`。照 14 写的界面会把这些画成一个对象。 -pub const CONTROL_API_VERSION: u32 = 15; +/// +/// **16 把最后几处原因也换成了 [`Msg`]**:`KeySyncFailed.error`、`PricingStatus.error`、 +/// `TurnView.error`。照 15 写的界面会把它们画成一个对象。 +pub const CONTROL_API_VERSION: u32 = 16; #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "ts", derive(ts_rs::TS))] @@ -1600,7 +1603,7 @@ pub struct KeySynced { pub struct KeySyncFailed { pub client: String, pub name: String, - pub error: String, + pub error: Msg, } /// 保存监听设置(`PUT /listen`)。 @@ -1897,7 +1900,7 @@ pub struct PricingStatus { pub checked_at_ms: Option, /// 最近一次刷新失败的原因 #[serde(default, skip_serializing_if = "Option::is_none")] - pub error: Option, + pub error: Option, /// 最近 7 天里**无法计价**的请求数。 /// /// 用户不会主动想起要配价格,只有「有 37 次请求无法计价」这种具体 @@ -3153,7 +3156,7 @@ pub struct TurnView { /// **没有价格就是 None,不是 0** pub cost_micros: Option, pub duration_ms: Option, - pub error: Option, + pub error: Option, /// 客户端没等到这一轮结束就走了(见 `HistoryRow::cancelled`) pub cancelled: bool, /// 这一轮的金额是估算。**瀑布图上要带记号** —— 以前这里没有这个字段, diff --git a/crates/tw-control/src/chatgpt.rs b/crates/tw-control/src/chatgpt.rs index b22c9ecd..24a4d5c2 100644 --- a/crates/tw-control/src/chatgpt.rs +++ b/crates/tw-control/src/chatgpt.rs @@ -948,11 +948,13 @@ async fn send( } req.send().await.map_err(|e| { let why = tw_gateway::forward::map_reqwest_error(e); + // 连不上的那句数据面自己带码,这里只补上「连的是 ChatGPT 后端」 fail( StatusCode::BAD_GATEWAY, - msg!( - "control.chatgpt_backend_unreachable", detail = why.message() => - "The ChatGPT backend could not be reached: {detail}" + why.detail.in_context( + "backend", + "chatgpt", + "The ChatGPT backend could not be reached", ), ) }) diff --git a/crates/tw-control/src/keys.rs b/crates/tw-control/src/keys.rs index d9de80fa..b94c4c25 100644 --- a/crates/tw-control/src/keys.rs +++ b/crates/tw-control/src/keys.rs @@ -338,7 +338,7 @@ async fn rotate( Err(e) => failed.push(tw_api::KeySyncFailed { client: c.id.to_string(), name: c.name.to_string(), - error: e.to_string(), + error: e.msg(), }), } } diff --git a/crates/tw-control/src/pricing.rs b/crates/tw-control/src/pricing.rs index 5af84c52..e155cfc7 100644 --- a/crates/tw-control/src/pricing.rs +++ b/crates/tw-control/src/pricing.rs @@ -167,7 +167,7 @@ impl Default for Schedule { #[derive(Debug, Clone, Default)] struct Attempt { at_ms: Option, - error: Option, + error: Option, } /// 刷新默认价目表没成。 @@ -247,7 +247,7 @@ impl Updater { fn record(&self, r: &Result, at: SystemTime) { if let Ok(mut last) = self.last.lock() { last.at_ms = Some(millis(at)); - last.error = r.as_ref().err().map(ToString::to_string); + last.error = r.as_ref().err().map(RefreshError::msg); } } diff --git a/crates/tw-control/tests/pricing.rs b/crates/tw-control/tests/pricing.rs index 250ef697..e6647a2b 100644 --- a/crates/tw-control/tests/pricing.rs +++ b/crates/tw-control/tests/pricing.rs @@ -500,7 +500,12 @@ async fn a_failed_refresh_keeps_the_table_and_reports_why() { assert!(r["text"].as_str().unwrap().contains(says), "{r}"); let (_, status) = call(&b.app, "GET", "/pricing", serde_json::json!(null)).await; assert_eq!(status["source"], "builtin"); - assert!(status["error"].as_str().unwrap().contains(says), "{status}"); + // 和刷新那一次回的是同一句,带着同一个码 + assert_eq!(status["error"]["code"], r["code"], "{status}"); + assert!( + status["error"]["text"].as_str().unwrap().contains(says), + "{status}" + ); assert!(!b.data_file().exists()); } } diff --git a/crates/tw-store/src/db.rs b/crates/tw-store/src/db.rs index c4c51e42..96bcf29a 100644 --- a/crates/tw-store/src/db.rs +++ b/crates/tw-store/src/db.rs @@ -414,7 +414,8 @@ pub struct TurnRow { pub cache_read_tokens: Option, pub cost_micros: Option, pub duration_ms: Option, - pub error: Option, + /// 失败的原因,带着码 + pub error: Option, pub cancelled: bool, /// 这一轮的金额是估算。**瀑布图上要带记号** pub cost_estimated: bool, @@ -493,7 +494,7 @@ impl Db { let mut st = self.conn.prepare( "SELECT id, at_ms, model, provider, input_tokens, output_tokens, cache_read_tokens, cost_micros, duration_ms, error, cancelled, - cost_estimated, billing + cost_estimated, billing, error_code, error_args FROM requests WHERE session = ?1 AND local = 0 ORDER BY at_ms, id", )?; let rows = st.query_map([session], |r| { @@ -507,7 +508,7 @@ impl Db { cache_read_tokens: r.get(6)?, cost_micros: r.get(7)?, duration_ms: r.get(8)?, - error: r.get(9)?, + error: error_from(r)?, cancelled: r.get::<_, i64>(10)? != 0, cost_estimated: r.get::<_, i64>(11)? != 0, billing: slug_col(r, 12, tw_api::Billing::from_slug)?,