fix(chat-channel): detect Weixin auth failure via errcode during connect verification

This commit is contained in:
xintaofei
2026-04-17 21:09:31 +08:00
parent 0b102f0120
commit 9abb85b8ee

View File

@@ -483,13 +483,24 @@ impl ChatChannelBackend for WeixinBackend {
ChatChannelError::ConnectionFailed(format!("JSON parse failed: {e}")) ChatChannelError::ConnectionFailed(format!("JSON parse failed: {e}"))
})?; })?;
let ret = verify_result // iLink API auth failures come back as `{"errcode":-14,"errmsg":"session timeout"}`
.get("ret") // (no `ret` field). Treat any non-zero errcode as authentication failure.
.and_then(|v| v.as_i64()) if let Some(errcode) = verify_result.get("errcode").and_then(|v| v.as_i64()) {
.unwrap_or(-1); if errcode != 0 {
let errmsg = verify_result
.get("errmsg")
.and_then(|v| v.as_str())
.unwrap_or("unknown error");
return Err(ChatChannelError::AuthenticationFailed(format!(
"Weixin verification failed (errcode={errcode}): {errmsg}"
)));
}
}
let ret = verify_result.get("ret").and_then(|v| v.as_i64());
// Check for known auth-failure codes // Check for known auth-failure codes
if ret == -14 { if ret == Some(-14) {
return Err(ChatChannelError::AuthenticationFailed( return Err(ChatChannelError::AuthenticationFailed(
"Session expired (ret=-14), please re-authenticate".into(), "Session expired (ret=-14), please re-authenticate".into(),
)); ));
@@ -503,11 +514,13 @@ impl ChatChannelBackend for WeixinBackend {
.unwrap_or("") .unwrap_or("")
.to_string(); .to_string();
if ret != 0 { if let Some(r) = ret {
eprintln!( if r != 0 {
"[Weixin] verify returned ret={ret}, but got cursor len={}", eprintln!(
initial_cursor.len() "[Weixin] verify returned ret={r}, but got cursor len={}",
); initial_cursor.len()
);
}
} }
*self.status.lock().await = ChannelConnectionStatus::Connected; *self.status.lock().await = ChannelConnectionStatus::Connected;