Skip to main content

wx_rust_common/error/
mod.rs

1//! 错误模型。
2//!
3//! 对应 Java `me.chanjar.weixin.common.error` 包。
4//! 提供 `WxError`(错误码对象)、统一错误枚举 [`WxErrorException`] 与分平台错误码翻译表。
5
6pub mod wx_channel_error_msg_enum;
7pub mod wx_cp_error_msg_enum;
8pub mod wx_error;
9pub mod wx_error_exception;
10pub mod wx_ma_error_msg_enum;
11pub mod wx_mp_error_msg_enum;
12pub mod wx_open_error_msg_enum;
13pub mod wx_runtime_exception;
14
15pub use wx_error::WxError;
16pub use wx_error_exception::{DEFAULT_ERROR_CODE, WxErrorError};
17pub use wx_runtime_exception::WxRuntimeError;
18
19use crate::enums::WxType;
20
21/// 微信接口调用统一错误类型。
22///
23/// 对应 Java `WxErrorException`(checked)与 `WxRuntimeException`(unchecked)的合并;
24/// 所有 Service 方法以 `Result<T, WxErrorException>` 返回。
25#[derive(Debug, Clone, thiserror::Error)]
26pub enum WxErrorException {
27    /// 微信接口返回的业务错误(对应 Java `WxErrorException(WxError)`)
28    #[error("{0}")]
29    Wx(#[from] WxErrorError),
30
31    /// 运行时错误(对应 Java `WxRuntimeException`:重试超限、token 超时等)
32    #[error("{0}")]
33    Runtime(#[from] WxRuntimeError),
34
35    /// IO 错误
36    #[error("IO 错误: {0}")]
37    Io(String),
38
39    /// HTTP 请求错误
40    #[error("HTTP 错误: {0}")]
41    Http(String),
42
43    /// 序列化错误
44    #[error("序列化错误: {0}")]
45    Serde(String),
46}
47
48impl WxErrorException {
49    /// 返回微信错误码;非业务错误时返回 `None`。
50    pub fn error_code(&self) -> Option<i32> {
51        match self {
52            WxErrorException::Wx(e) => Some(e.error.error_code),
53            _ => None,
54        }
55    }
56
57    /// 返回微信错误对象引用(仅业务错误变体)。
58    pub fn wx_error(&self) -> Option<&WxError> {
59        match self {
60            WxErrorException::Wx(e) => Some(&e.error),
61            _ => None,
62        }
63    }
64
65    /// 由微信错误码与错误信息构建业务错误。
66    pub fn from_code(code: i32, msg: impl Into<String>) -> Self {
67        WxErrorException::Wx(WxErrorError::new(WxError::new(code, msg)))
68    }
69}
70
71impl From<std::io::Error> for WxErrorException {
72    fn from(e: std::io::Error) -> Self {
73        WxErrorException::Io(e.to_string())
74    }
75}
76
77impl From<reqwest::Error> for WxErrorException {
78    fn from(e: reqwest::Error) -> Self {
79        WxErrorException::Http(e.to_string())
80    }
81}
82
83impl From<serde_json::Error> for WxErrorException {
84    fn from(e: serde_json::Error) -> Self {
85        WxErrorException::Serde(e.to_string())
86    }
87}
88
89/// 按平台查找错误码的中文翻译。
90///
91/// # 参数
92/// - `wx_type`:微信平台类型
93/// - `code`:微信错误码
94///
95/// # 返回
96/// 该平台错误码对应的中文信息;未收录时返回 `None`。
97pub fn translate_error_msg(wx_type: WxType, code: i32) -> Option<&'static str> {
98    match wx_type {
99        WxType::Mp => wx_mp_error_msg_enum::find_msg_by_code(code),
100        WxType::Cp => wx_cp_error_msg_enum::find_msg_by_code(code),
101        WxType::MiniApp => wx_ma_error_msg_enum::find_msg_by_code(code),
102        WxType::Open => wx_open_error_msg_enum::find_msg_by_code(code),
103        WxType::Channel => wx_channel_error_msg_enum::find_msg_by_code(code),
104        // 支付无独立错误码翻译表(与 Java 一致:Pay 走 default 分支不翻译)
105        WxType::Pay => None,
106    }
107}