Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.worktrees/
24 changes: 10 additions & 14 deletions src/wechat/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,9 @@ impl fmt::Display for ApiError {

impl StdError for ApiError {}

#[derive(Debug)]
pub struct SessionExpiredError;

impl fmt::Display for SessionExpiredError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Session expired")
}
}

impl StdError for SessionExpiredError {}

pub fn is_session_expired(err: &anyhow::Error) -> bool {
err.is::<SessionExpiredError>()
err.downcast_ref::<ApiError>()
.is_some_and(|e| e.code == SESSION_EXPIRED_ERRCODE)
}

pub(crate) fn build_http_client() -> Client {
Expand Down Expand Up @@ -198,7 +188,11 @@ impl WeixinApiClient {
// Priority 1: errcode (often for session/auth errors)
if let Some(code) = status.errcode {
if code == SESSION_EXPIRED_ERRCODE {
return Err(SessionExpiredError.into());
return Err(ApiError {
code,
message: "Invalid bot token".to_string(),
}
.into());
}
if code != 0 {
return Err(ApiError {
Expand Down Expand Up @@ -344,8 +338,10 @@ mod tests {
let res: Result<EmptyResponse> = WeixinApiClient::decode_response(bytes);
assert!(res.is_err());
let err = res.unwrap_err();
assert!(err.is::<ApiError>());
assert!(is_session_expired(&err));
assert!(err.to_string().contains("Session expired"));
assert!(err.to_string().contains("Invalid bot token"));
assert!(err.to_string().contains("-14"));
}

#[test]
Expand Down
Loading