1#[allow(dead_code)]
2use serde::{Deserialize, Serialize};
3use std::error::Error as StdError;
4use std::fmt::{self, Debug, Display};
5use std::num::ParseIntError;
6use std::string::FromUtf8Error;
7use validator::ValidationErrors;
8
9pub type Result<T> = std::result::Result<T, Error>;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[non_exhaustive]
13#[serde(rename_all = "camelCase")]
14pub struct Error {
15 pub error_code: i32,
16 pub error_msg: String,
17}
18
19macro_rules! static_errors {
20 ($name:ident, $code:expr, $msg:expr) => {
21 #[allow(non_snake_case, missing_docs)]
22 pub fn $name() -> Error {
23 Error::create($code, $msg)
24 }
25 };
26}
27
28#[allow(dead_code)]
29impl Error {
30 pub fn create(code: i32, msg: &str) -> Self {
31 let err = Self {
32 error_code: code,
33 error_msg: msg.to_string(),
34 };
35 return err;
36 }
37 pub fn new() -> Self {
38 let err = Self {
39 error_code: 500,
40 error_msg: "".to_string(),
41 };
42 return err;
43 }
44 pub fn error_code(mut self, code: i32) -> Self {
45 self.error_code = code;
46 self
47 }
48 pub fn error_msg(mut self, msg: String) -> Self {
49 self.error_msg = msg;
50 self
51 }
52 static_errors!(NotFound, 404, "Not Found");
55 static_errors!(ErrorNoPermission, 403, "No permission management \n 沒有權限管理");
56 static_errors!(ErrorInternalServer, 500, "Internal Server Error \n 服务器内部错误");
57 static_errors!(ErrorRedisConnectError, 6000, "Failed to connect to Redis \n Redis 连接失败");
58
59 static_errors!(ErrorStartTransactionFailed, 6001, "Failed to open transaction \n 開啟事務失敗");
60 static_errors!(ErrorCommitTransactionFailed, 6002, "Failed to commit transaction \n 提交事務失敗");
61 static_errors!(ErrorParsingPrivateKey, 1406, "Error parsing private key \n 解析私钥错误");
62 static_errors!(NotFoundPrivateKey, 1405, "Not Found Private Key \n 没有找到私钥");
63
64
65 static_errors!(ErrorCreateFailed, 8000, "Creation operation failed. \n 创建操作失败。");
67 static_errors!(ErrorStaffRepeatCreate, 8001, "The current staff member has already been created. \n 当前员工已创建。");
68 static_errors!(ErrorAMSCreateStaffFailed, 8002, "Failed to create a staff member in AMS. \n AMS 创建员工失败。");
69
70 static_errors!(ErrorUpdateFailed, 8003, "Update operation failed. \n 修改操作失败。");
72 static_errors!(ErrorAMSUpdateStaffFailed, 8004, "Failed to update a staff member in AMS. \n AMS 更新员工失败。");
73
74 static_errors!(ErrorDeleteFailed, 8005, "Deletion operation failed. \n 删除操作失败。");
76 static_errors!(ErrorDeleteStaffCardFailed, 8006, "Failed to delete the staff member's card. Please contact the administrator! \n 删除人员卡失败,请联系管理员!");
77 static_errors!(ErrorDeleteStaffFailed, 8007, "Failed to delete the staff member's roles. Please contact the administrator! \n 删除人员权限失败,请联系管理员!");
78
79 static_errors!(ErrorQueryFailed, 8008, "Query operation failed. \n 查询操作失败。");
81 static_errors!(ErrorRemoteRequestFailed, 8009, "Remote request failed. \n 远程请求失败。");
83 static_errors!(ErrorAMSDeviceStatusInterface, 8010, "Failed to call the AMS device status interface. \n 调用 AMS 设备状态接口失败。");
85
86 static_errors!(ErrorAMSSyncCommandTypeInterface, 8011, "Failed to synchronize the AMS command type interface. \n 同步 AMS 命令类型接口失败。");
88 static_errors!(ErrorAMSDeviceSyncInterface, 8012, "Failed to synchronize the AMS device interface. \n 同步 AMS 设备接口失败。");
89 static_errors!(ErrorAMSDeviceTypeSyncInterface, 8013, "Failed to synchronize the AMS device type interface. \n 同步 AMS 设备类型接口失败。");
90 static_errors!(ErrorAMSEventTypeSyncInterface, 8014, "Failed to synchronize the AMS event type interface. \n 同步 AMS 事件类型接口失败。");
91 static_errors!(ErrorAMSPermissionSyncInterface, 8015, "Failed to synchronize the AMS permission interface. \n 同步 AMS 权限接口失败。");
92 static_errors!(ErrorAMSStatusTypeSyncInterface, 8016, "Failed to synchronize the AMS status type interface. \n 同步 AMS 状态类型失败。");
93
94 static_errors!(ErrorAMSPersonAuthorFailed, 8017, "Failed to authorize a card for a personnel in the AMS interface. \n AMS 人员授权卡失败。");
96 static_errors!(ErrorAMSPersonAuthorRepeat, 8018, "The personnel has already been authorized. Please do not authorize again! \n 人员已授权,请不要重复授权!");
97 static_errors!(ErrorAMSPersonAuthorRoleFailed, 8019, "Failed to authorize a role for a personnel in the AMS interface. \n AMS 人员授权角色失败。");
98
99 static_errors!(ErrorPersonnelDeviceUnauthorized, 8020, "The personnel is not authorized to use the device. \n 人员没有授权使用设备。");
101 static_errors!(ErrorAMSDeviceCommandExecution, 8021, "The AMS device failed to execute the command. \n AMS 设备执行命令失败。");
102
103 static_errors!(ErrorCardRepeatAssign, 8022, "The current card has already been assigned. \n 当前卡已分配。");
105 static_errors!(ErrorCardRepeatStaff, 8023, "The current card has already been assigned to a staff member. \n 当前卡已分配给人员。");
106 static_errors!(ErrorAMSUnbindCardFailed, 8024, "Failed to unbind a card in AMS. \n AMS 解绑卡接口失败。");
107 static_errors!(ErrorQRCodeGenerateWithoutAuth, 8025, "Without proper authorization, unable to generate a QR code. \n 没有授权,无法生成二维码!");
108 static_errors!(ErrorPermissionExpiryConfigMissing, 8026, "Configuration error: The permission expiration time is not configured. \n 配置错误,权限没有配置过期时间!");
109 static_errors!(ErrorCardNumberDuplicate, 8027, "The card number is a duplicate. \n 卡号重复。");
110 static_errors!(ErrorDoorOpeningTimeRestricted, 8028, "Door opening is not allowed during the current time period. \n 当前时间段不允许开门。");
111 static_errors!(ErrorPersonValidTimeNotSet, 8029, "The person's valid time must be set. \n 人员有效时间必须设置。");
113 static_errors!(ErrorPersonValidTimeInvalid, 8030, "The person's valid time is invalid. \n 人员有效时间无效。");
115 static_errors!(ErrorPersonNotFound, 8031, "The person is not found. \n 没有查找到人员信息。");
117 static_errors!(ErrorAMSDeletePermissionFailed, 8032, "Failed to delete a permission in AMS. \n AMS 删除人员权限失败。");
119 static_errors!(ErrorDepartmentNameRepeat, 8033, "The department name already exists. \n 部门名称已存在。");
121 static_errors!(ErrorParentNodeNotExist, 8034, "The parent node does not exist. \n 父节点不存在。");
123 static_errors!(ErrorDepartmentDisable, 8035, "The department is disabled. \n 部门停用,不允许新增。");
125 static_errors!(ErrorDataNotFound, 8036, "No data found. \n 没有找到数据。");
127 static_errors!(ErrorPostHasStaff, 8037, "The post has staff members, and it is not allowed to delete. \n 岗位已分配人员,不允许删除。");
129
130
131
132}
133
134impl Display for Error {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 write!(f, "{}:{}", self.error_code, self.error_msg)
137 }
138}
139impl StdError for Error {}
140
141impl From<&str> for Error {
142 fn from(arg: &str) -> Self {
143 Error::new().error_msg(arg.to_string())
144 }
145}
146
147impl From<std::io::Error> for Error {
149 fn from(arg: std::io::Error) -> Self {
150 tracing::error!("std::io::Error:{}",arg.to_string());
151 Error::new().error_code(100).error_msg(arg.to_string())
152 }
153}
154
155impl From<sea_orm::DbErr> for Error {
157 fn from(arg: sea_orm::DbErr) -> Self {
158 tracing::error!("sea_orm::DbErr:{}",arg.to_string());
159 Error::new().error_code(101).error_msg(arg.to_string())
160 }
161}
162
163impl From<serde_json::Error> for Error {
165 fn from(arg: serde_json::Error) -> Self {
166 tracing::error!("serde_json::Error:{}",arg.to_string());
167 Error::new().error_code(102).error_msg(arg.to_string())
168 }
169}
170
171impl From<reqwest::Error> for Error {
173 fn from(arg: reqwest::Error) -> Self {
174 tracing::error!("reqwest::Error:{}",arg.to_string());
175 Error::new().error_code(103).error_msg(arg.to_string())
176 }
177}
178
179
180impl From<redis::RedisError> for Error {
182 fn from(arg: redis::RedisError) -> Self {
183 tracing::error!("RedisError:{}",arg.to_string());
184 Error::new().error_code(105).error_msg(arg.to_string())
185 }
186}
187
188
189impl From<ValidationErrors> for Error {
191 fn from(arg: ValidationErrors) -> Self {
192 tracing::error!("ValidationErrors:{}",arg.to_string());
193 Error::new().error_code(106).error_msg(arg.to_string())
194 }
195}
196
197impl From<ParseIntError> for Error {
199 fn from(arg: ParseIntError) -> Self {
200 tracing::error!("ParseIntError:{}",arg.to_string());
201 Error::new().error_code(107).error_msg(arg.to_string())
202 }
203}
204
205impl From<FromUtf8Error> for Error {
207 fn from(arg: FromUtf8Error) -> Self {
208 tracing::error!("FromUtf8Error:{}",arg.to_string());
209 Error::new().error_code(108).error_msg(arg.to_string())
210 }
211}
212