WeChatError

Enum WeChatError 

Source
pub enum WeChatError {
Show 18 variants Configuration(String), Transport(TransportError), ResponseParseFailed { url: String, error: String, preview: String, }, ApiError { errcode: i32, errmsg: String, }, AccessTokenExpired { code: i32, }, AccessTokenMissing, TokenInvalidated, InvalidCode, CodeUsed, CodeExpired, RefreshTokenInvalid, RefreshTokenExpired, UserUnauthorized, RateLimitExceeded { code: i32, msg: String, }, IpNotWhitelisted, SystemBusy, InvalidParameter(String), HttpBuildError(String),
}
Expand description

微信授权错误类型

Variants§

§

Configuration(String)

配置错误

§

Transport(TransportError)

网络传输错误

§

ResponseParseFailed

响应解析失败

Fields

§error: String
§preview: String
§

ApiError

微信API通用错误

Fields

§errcode: i32
§errmsg: String
§

AccessTokenExpired

Access Token过期或无效

错误码: 40001, 40014, 42001

解决方案:

  1. 检查AppSecret是否正确
  2. 使用refresh_token刷新access_token
  3. 如果refresh_token也过期,需要重新授权

Fields

§code: i32
§

AccessTokenMissing

Access Token缺失

错误码: 41001

解决方案: 检查API调用时是否传递了access_token参数

§

TokenInvalidated

用户修改密码导致Token失效

错误码: 42007

原因: 用户修改了微信密码,导致之前授权的所有token失效

解决方案: 引导用户重新进行授权

§

InvalidCode

无效的授权码

错误码: 40029

原因:

  • 授权码格式错误
  • 授权码不是通过微信授权流程获得
  • AppID与授权码不匹配

解决方案: 检查授权流程是否正确,确保使用正确的AppID

§

CodeUsed

授权码已被使用

错误码: 40163

原因:

  • 授权码(code)只能使用一次
  • 页面刷新或重复请求导致code被重复使用
  • 服务端错误导致回调接口被调用多次

解决方案:

  1. 检查前端是否有重复请求
  2. 确保回调接口没有抛出异常
  3. 避免页面刷新时重复使用旧的code
  4. 让用户重新进行授权获取新的code
§

CodeExpired

授权码已过期

错误码: 42003

原因: 授权码(code)的有效期为5分钟,超时后无法使用

解决方案: 引导用户重新进行授权获取新的code

§

RefreshTokenInvalid

Refresh Token无效

错误码: 40030

解决方案: 引导用户重新授权

§

RefreshTokenExpired

Refresh Token已过期

错误码: 42002

原因: refresh_token的有效期为30天,超时后无法使用

解决方案: 引导用户重新进行授权

§

UserUnauthorized

用户未授权该API

错误码: 50001

原因:

  • 授权作用域不足
  • 账号类型不支持该API
  • 未配置授权回调域名

解决方案:

  1. 检查授权时的scope参数
  2. 确认账号类型是否支持(如需要认证的服务号)
  3. 在微信后台配置授权回调域名
§

RateLimitExceeded

API调用频率超限

错误码: 45009(分钟级), 45011(日级)

解决方案:

  1. 实施请求限流
  2. 使用缓存减少API调用
  3. 等待限制解除后重试

Fields

§code: i32
§

IpNotWhitelisted

服务器IP未在白名单中

错误码: 40164

解决方案: 在微信公众平台后台添加服务器IP到白名单

§

SystemBusy

微信系统繁忙

错误码: -1

解决方案: 这是临时性错误,等待后重试即可

§

InvalidParameter(String)

参数错误

错误码: 61451

解决方案: 检查API调用参数是否正确

§

HttpBuildError(String)

HTTP构建错误

Implementations§

Source§

impl WeChatError

Source

pub fn is_retriable(&self) -> bool

判断错误是否可以重试

§返回值
  • true: 错误是临时性的,可以重试
  • false: 错误需要人工介入,重试无效
§示例
if error.is_retriable() {
    // 等待后重试
    println!("可以重试");
} else {
    println!("无法重试,需要人工处理");
}
Source

pub fn requires_reauthorization(&self) -> bool

判断错误是否需要重新授权

§返回值
  • true: 需要引导用户重新进行微信授权
  • false: 不需要重新授权
§示例
if error.requires_reauthorization() {
    // 清除本地token缓存
    // 返回授权URL给前端
    println!("需要重新授权");
}
Source

pub fn is_configuration_error(&self) -> bool

判断错误是否是配置问题

§返回值
  • true: 是配置问题,需要修改配置
  • false: 不是配置问题
§示例
if error.is_configuration_error() {
    // 记录配置错误日志
    // 发送告警通知
    eprintln!("配置错误: {}", error);
}
Source

pub fn recommended_retry_delay(&self) -> Option<u64>

获取错误的推荐重试延迟(秒)

§返回值
  • Some(seconds): 建议延迟的秒数
  • None: 不建议重试
§示例
if let Some(delay) = error.recommended_retry_delay() {
    println!("建议{}秒后重试", delay);
    // tokio::time::sleep(Duration::from_secs(delay)).await;
}

Trait Implementations§

Source§

impl Debug for WeChatError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for WeChatError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for WeChatError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Error> for WeChatError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<TransportError> for WeChatError

Source§

fn from(source: TransportError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more