Skip to main content

bpi_rs/err/
code.rs

1use super::error::ErrorCategory;
2use std::collections::HashMap;
3use std::sync::OnceLock;
4
5// 错误码映射表
6static ERROR_MAP: OnceLock<HashMap<i32, ErrorInfo>> = OnceLock::new();
7
8#[derive(Clone)]
9struct ErrorInfo {
10    message: &'static str,
11    category: ErrorCategory,
12}
13
14impl ErrorInfo {
15    fn new(message: &'static str, category: ErrorCategory) -> Self {
16        Self { message, category }
17    }
18}
19
20pub fn get_error_message(code: i32) -> String {
21    get_error_map()
22        .get(&code)
23        .map(|info| info.message.to_string())
24        .unwrap_or_else(|| "未知错误".to_string())
25}
26
27pub fn categorize_error(code: i32) -> ErrorCategory {
28    get_error_map()
29        .get(&code)
30        .map(|info| info.category.clone())
31        .unwrap_or(ErrorCategory::Unknown)
32}
33
34// 宏:自动生成错误码映射
35macro_rules! define_errors {
36  ($($category:ident: [$(($code:expr, $msg:expr)),* $(,)?]),* $(,)?) => {
37        fn get_error_map() -> &'static HashMap<i32, ErrorInfo> {
38            ERROR_MAP.get_or_init(|| {
39                let mut map = HashMap::new();
40
41                $(
42                    // 为每个分类添加错误码
43                    $(
44                        map.insert($code, ErrorInfo::new($msg, ErrorCategory::$category));
45                    )*
46                )*
47
48                map
49            })
50        }
51  };
52}
53
54// 使用宏定义所有错误码
55define_errors! {
56    Auth: [
57        (-1, "应用程序不存在或已被封禁"),
58        (-2, "Access Key 错误"),
59        (-3, "API 校验密匙错误"),
60        (-4, "调用方对该 Method 没有权限"),
61        (-101, "账号未登录"),
62        (-102, "账号被封停"),
63        (-105, "验证码错误"),
64        (-107, "应用不存在或者被封禁"),
65        (-108, "未绑定手机"),
66        (-110, "未绑定手机"),
67        (-111, "csrf 校验失败"),
68        (-113, "账号尚未实名认证"),
69        (-114, "请先绑定手机"),
70        (-115, "请先完成实名认证"),
71    ],
72
73    Business: [
74        (-103, "积分不足"),
75        (-104, "硬币不足"),
76        (-106, "账号非正式会员或在适应期"),
77        (-509, "超出限制"),
78        (-650, "用户等级太低"),
79        (-688, "地理区域限制"),
80        (-689, "版权限制"),
81        (-701, "扣节操失败"),
82    ],
83
84    Request: [
85        (-304, "木有改动"),
86        (-307, "撞车跳转"),
87        (-352, "风控校验失败 (UA 或 wbi 参数不合法)"),
88        (-400, "请求错误"),
89        (-401, "未认证 (或非法请求)"),
90        (-403, "访问权限不足"),
91        (-404, "啥都木有"),
92        (-405, "不支持该方法"),
93        (-409, "冲突"),
94        (-412, "请求被拦截 (客户端 ip 被服务端风控)"),
95        (-616, "上传文件不存在"),
96        (-617, "上传文件太大"),
97        (-625, "登录失败次数太多"),
98        (-626, "用户不存在"),
99        (-628, "密码太弱"),
100        (-629, "用户名或密码错误"),
101        (-632, "操作对象数量限制"),
102        (-643, "被锁定"),
103        (-652, "重复的用户"),
104        (-658, "Token 过期"),
105        (-662, "密码时间戳过期"),
106    ],
107
108    Server: [
109        (-112, "系统升级中"),
110        (-500, "服务器错误"),
111        (-503, "过载保护,服务暂不可用"),
112        (-504, "服务调用超时"),
113        (-799, "请求过于频繁,请稍后再试"),
114        (-8888, "对不起,服务器开小差了~ (ಥ﹏ಥ)"),
115    ],
116}