wx_rust_common/error/
mod.rs1pub 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#[derive(Debug, Clone, thiserror::Error)]
26pub enum WxErrorException {
27 #[error("{0}")]
29 Wx(#[from] WxErrorError),
30
31 #[error("{0}")]
33 Runtime(#[from] WxRuntimeError),
34
35 #[error("IO 错误: {0}")]
37 Io(String),
38
39 #[error("HTTP 错误: {0}")]
41 Http(String),
42
43 #[error("序列化错误: {0}")]
45 Serde(String),
46}
47
48impl WxErrorException {
49 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 pub fn wx_error(&self) -> Option<&WxError> {
59 match self {
60 WxErrorException::Wx(e) => Some(&e.error),
61 _ => None,
62 }
63 }
64
65 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
89pub 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 WxType::Pay => None,
106 }
107}