1use super::traits::ErrorSeverity;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub enum ErrorCode {
15 Success = 0,
18
19 BadRequest = 400,
22 Unauthorized = 401,
24 Forbidden = 403,
26 NotFound = 404,
28 MethodNotAllowed = 405,
30 Conflict = 409,
32 TooManyRequests = 429,
34
35 InternalServerError = 500,
38 BadGateway = 502,
40 ServiceUnavailable = 503,
42 GatewayTimeout = 504,
44
45 AppTicketInvalid = 10012,
48 AppStatusException = 10013,
50 AppPermissionDenied = 19001,
52 AccessTokenInvalid = 99991671,
54 AccessTokenFormatInvalid = 99991661,
56 AppAccessTokenInvalid = 99991664,
58 TenantAccessTokenInvalid = 99991663,
60 SsoTokenInvalid = 99991670,
62 PermissionMissing = 99991672,
64 AccessTokenNoPermission = 99991676,
66 AccessTokenExpiredV2 = 99991677,
68
69 UserSessionInvalid = 99991641,
72 UserSessionNotFound = 99991642,
74 UserSessionTimeout = 99991645,
76 UserIdentityInvalid = 99991669,
78 UserTypeNotSupportedV2 = 99991674,
80 UserIdentityMismatch = 99991675,
82 UserIdInvalid = 99992351,
84 OpenIdInvalid = 99992352,
86 UnionIdInvalid = 99992353,
88
89 AppNotInstalled = 10003,
92 UserNotFound = 60001,
94 UserStatusException = 60002,
96 DepartmentNotFound = 60003,
98 ChatNotFound = 70001,
100 ChatTypeNotSupported = 70002,
102 MessageNotFound = 80001,
104 MessageTypeNotSupported = 80002,
106 FileNotFound = 90001,
108 FileSizeExceeded = 90002,
110 FileTypeNotSupported = 90003,
112
113 CalendarNotFound = 110001,
116 EventNotFound = 110002,
118 EventConflict = 110003,
120 DocumentNotFound = 120001,
122 DocumentPermissionDenied = 120002,
124 DocumentLocked = 120003,
126 SheetNotFound = 120011,
128 TableNotFound = 120021,
130
131 NetworkTimeout = 999001,
134 NetworkConnectionFailed = 999002,
136 DnsResolutionFailed = 999003,
138 SslCertificateError = 999004,
140 ConnectionRefused = 999005,
142
143 SerializationError = 999010,
146 DataFormatError = 999011,
148 EncodingError = 999012,
150
151 AuthenticationFailed = 999020,
154 PermissionDenied = 999021,
156 TokenExpired = 999022,
158 InvalidSignature = 999023,
160
161 ValidationError = 999030,
164 MissingRequiredParameter = 999031,
166 InvalidParameterFormat = 999032,
168 ParameterOutOfRange = 999033,
170
171 BusinessError = 999040,
174 OperationNotSupported = 999041,
176 ResourceConflict = 999042,
178
179 InternalError = 999050,
182 ConfigurationError = 999051,
184 ResourceExhausted = 999052,
186
187 RateLimitExceeded = 999060,
190
191 ResponseTooLarge = 41300,
194
195 CacheMiss = 999070,
198 CacheServiceUnavailable = 999071,
200
201 SecurityPolicyViolation = 999080,
204 AccessDenied = 999081,
206
207 Unknown = 999999,
210}
211
212impl ErrorCode {
213 pub fn from_code(code: i32) -> Self {
215 match code {
216 c if (200..=299).contains(&c) => Self::Success,
217 0 => Self::Success,
218
219 400 => Self::BadRequest,
221 401 => Self::Unauthorized,
222 403 => Self::Forbidden,
223 404 => Self::NotFound,
224 405 => Self::MethodNotAllowed,
225 409 => Self::Conflict,
226 429 => Self::TooManyRequests,
227
228 500 => Self::InternalServerError,
230 502 => Self::BadGateway,
231 503 => Self::ServiceUnavailable,
232 504 => Self::GatewayTimeout,
233
234 10003 => Self::AppNotInstalled,
236 10012 => Self::AppTicketInvalid,
237 10013 => Self::AppStatusException,
238 19001 => Self::AppPermissionDenied,
239 60001 => Self::UserNotFound,
240 60002 => Self::UserStatusException,
241 60003 => Self::DepartmentNotFound,
242 70001 => Self::ChatNotFound,
243 70002 => Self::ChatTypeNotSupported,
244 80001 => Self::MessageNotFound,
245 80002 => Self::MessageTypeNotSupported,
246 90001 => Self::FileNotFound,
247 90002 => Self::FileSizeExceeded,
248 90003 => Self::FileTypeNotSupported,
249 110001 => Self::CalendarNotFound,
250 110002 => Self::EventNotFound,
251 110003 => Self::EventConflict,
252 120001 => Self::DocumentNotFound,
253 120002 => Self::DocumentPermissionDenied,
254 120003 => Self::DocumentLocked,
255 120011 => Self::SheetNotFound,
256 120021 => Self::TableNotFound,
257 99991661 => Self::AccessTokenFormatInvalid,
258 99991671 => Self::AccessTokenInvalid,
259 99991664 => Self::AppAccessTokenInvalid,
260 99991663 => Self::TenantAccessTokenInvalid,
261 99991670 => Self::SsoTokenInvalid,
262 99991672 => Self::PermissionMissing,
263 99991676 => Self::AccessTokenNoPermission,
264 99991677 => Self::AccessTokenExpiredV2,
265 99991641 => Self::UserSessionInvalid,
266 99991642 => Self::UserSessionNotFound,
267 99991645 => Self::UserSessionTimeout,
268 99991669 => Self::UserIdentityInvalid,
269 99991674 => Self::UserTypeNotSupportedV2,
270 99991675 => Self::UserIdentityMismatch,
271 99992351 => Self::UserIdInvalid,
272 99992352 => Self::OpenIdInvalid,
273 99992353 => Self::UnionIdInvalid,
274
275 999001 => Self::NetworkTimeout,
277 999002 => Self::NetworkConnectionFailed,
278 999003 => Self::DnsResolutionFailed,
279 999004 => Self::SslCertificateError,
280 999005 => Self::ConnectionRefused,
281
282 999010 => Self::SerializationError,
284 999011 => Self::DataFormatError,
285 999012 => Self::EncodingError,
286
287 999020 => Self::AuthenticationFailed,
289 999021 => Self::PermissionDenied,
290 999022 => Self::TokenExpired,
291 999023 => Self::InvalidSignature,
292
293 999030 => Self::ValidationError,
295 999031 => Self::MissingRequiredParameter,
296 999032 => Self::InvalidParameterFormat,
297 999033 => Self::ParameterOutOfRange,
298
299 999040 => Self::BusinessError,
301 999041 => Self::OperationNotSupported,
302 999042 => Self::ResourceConflict,
303
304 999050 => Self::InternalError,
306 999051 => Self::ConfigurationError,
307 999052 => Self::ResourceExhausted,
308
309 999060 => Self::RateLimitExceeded,
311
312 41300 => Self::ResponseTooLarge,
314
315 999070 => Self::CacheMiss,
317 999071 => Self::CacheServiceUnavailable,
318
319 999080 => Self::SecurityPolicyViolation,
321 999081 => Self::AccessDenied,
322
323 _ => Self::Unknown,
324 }
325 }
326
327 pub fn as_code(&self) -> i32 {
329 *self as i32
330 }
331
332 pub fn description(&self) -> &'static str {
334 match self {
335 Self::Success => "操作成功",
336
337 Self::BadRequest => "请求参数错误",
339 Self::Unauthorized => "未认证",
340 Self::Forbidden => "禁止访问",
341 Self::NotFound => "资源不存在",
342 Self::MethodNotAllowed => "方法不允许",
343 Self::Conflict => "请求冲突",
344 Self::TooManyRequests => "请求频率过高",
345
346 Self::InternalServerError => "内部服务器错误",
348 Self::BadGateway => "网关错误",
349 Self::ServiceUnavailable => "服务不可用",
350 Self::GatewayTimeout => "网关超时",
351
352 Self::AppNotInstalled => "应用未安装",
354 Self::AppTicketInvalid => "应用票据无效",
355 Self::AppStatusException => "应用状态异常",
356 Self::AppPermissionDenied => "应用权限不足",
357 Self::UserNotFound => "用户不存在",
358 Self::UserStatusException => "用户状态异常",
359 Self::DepartmentNotFound => "部门不存在",
360 Self::ChatNotFound => "群组不存在",
361 Self::ChatTypeNotSupported => "群组类型不支持",
362 Self::MessageNotFound => "消息不存在",
363 Self::MessageTypeNotSupported => "消息类型不支持",
364 Self::FileNotFound => "文件不存在",
365 Self::FileSizeExceeded => "文件大小超限",
366 Self::FileTypeNotSupported => "文件类型不支持",
367 Self::CalendarNotFound => "日历不存在",
368 Self::EventNotFound => "日程不存在",
369 Self::EventConflict => "日程冲突",
370 Self::DocumentNotFound => "文档不存在",
371 Self::DocumentPermissionDenied => "文档权限不足",
372 Self::DocumentLocked => "文档已锁定",
373 Self::SheetNotFound => "工作表不存在",
374 Self::TableNotFound => "表格不存在",
375 Self::AccessTokenFormatInvalid => "访问令牌格式错误",
376 Self::AccessTokenInvalid => "访问令牌无效",
377 Self::AppAccessTokenInvalid => "应用访问令牌无效",
378 Self::TenantAccessTokenInvalid => "租户访问令牌无效",
379 Self::SsoTokenInvalid => "SSO 访问令牌无效",
380 Self::PermissionMissing => "缺少所需权限",
381 Self::AccessTokenNoPermission => "访问令牌权限不足",
382 Self::AccessTokenExpiredV2 => "访问令牌已过期",
383 Self::UserSessionInvalid => "用户会话已失效",
384 Self::UserSessionNotFound => "用户会话不存在",
385 Self::UserSessionTimeout => "用户会话超时",
386 Self::UserIdentityInvalid => "用户身份解析失败",
387 Self::UserTypeNotSupportedV2 => "用户类型不支持",
388 Self::UserIdentityMismatch => "用户身份不匹配",
389 Self::UserIdInvalid => "用户ID非法",
390 Self::OpenIdInvalid => "OpenID 非法",
391 Self::UnionIdInvalid => "UnionID 非法",
392
393 Self::NetworkTimeout => "网络连接超时",
395 Self::NetworkConnectionFailed => "网络连接失败",
396 Self::DnsResolutionFailed => "DNS解析失败",
397 Self::SslCertificateError => "SSL证书错误",
398 Self::ConnectionRefused => "连接被拒绝",
399
400 Self::SerializationError => "JSON解析错误",
402 Self::DataFormatError => "数据格式错误",
403 Self::EncodingError => "编码错误",
404
405 Self::AuthenticationFailed => "身份验证失败",
407 Self::PermissionDenied => "权限被拒绝",
408 Self::TokenExpired => "令牌已过期",
409 Self::InvalidSignature => "无效签名",
410
411 Self::ValidationError => "参数验证失败",
413 Self::MissingRequiredParameter => "缺少必需参数",
414 Self::InvalidParameterFormat => "参数格式错误",
415 Self::ParameterOutOfRange => "参数值超出范围",
416
417 Self::BusinessError => "业务逻辑错误",
419 Self::OperationNotSupported => "操作不被支持",
420 Self::ResourceConflict => "资源状态冲突",
421
422 Self::InternalError => "系统内部错误",
424 Self::ConfigurationError => "配置错误",
425 Self::ResourceExhausted => "资源耗尽",
426
427 Self::RateLimitExceeded => "请求频率限制",
429
430 Self::ResponseTooLarge => "响应体大小超过限制",
432
433 Self::CacheMiss => "缓存未命中",
435 Self::CacheServiceUnavailable => "缓存服务不可用",
436
437 Self::SecurityPolicyViolation => "安全策略违规",
439 Self::AccessDenied => "访问被拒绝",
440
441 Self::Unknown => "未知错误",
442 }
443 }
444
445 pub fn detailed_description(&self) -> &'static str {
447 match self {
448 Self::Success => "请求已成功处理",
449
450 Self::BadRequest => "请求参数格式错误或缺少必需参数,请检查请求内容",
451 Self::Unauthorized => "身份验证失败,请检查访问令牌是否有效",
452 Self::Forbidden => "权限不足,无法访问请求的资源",
453 Self::NotFound => "请求的资源不存在或已被删除",
454 Self::MethodNotAllowed => "当前API不支持此HTTP方法",
455 Self::Conflict => "请求与当前资源状态冲突,请检查资源状态",
456 Self::TooManyRequests => "请求频率超过限制,请降低请求频率后重试",
457
458 Self::InternalServerError => "服务器内部错误,请稍后重试或联系技术支持",
459 Self::BadGateway => "网关服务器错误,请检查网络连接或稍后重试",
460 Self::ServiceUnavailable => "服务暂时不可用,请稍后重试",
461 Self::GatewayTimeout => "网关超时,请稍后重试",
462
463 Self::AppNotInstalled => "应用未安装到当前企业,请在飞书管理后台安装应用",
464 Self::AppTicketInvalid => "应用票据已失效,SDK会自动重新申请",
465 Self::AppStatusException => "应用状态异常,请检查应用是否正常启用",
466 Self::AppPermissionDenied => "应用缺少执行此操作的权限,请在开发者后台配置相应权限",
467 Self::UserNotFound => "指定的用户不存在,请检查用户ID是否正确",
468 Self::UserStatusException => "用户状态异常,可能已被禁用或删除",
469 Self::DepartmentNotFound => "指定的部门不存在,请检查部门ID是否正确",
470 Self::ChatNotFound => "指定的群组不存在或机器人未加入该群组",
471 Self::ChatTypeNotSupported => "当前群组类型不支持此操作",
472 Self::MessageNotFound => "指定的消息不存在或已被删除",
473 Self::MessageTypeNotSupported => "不支持的消息类型",
474 Self::FileNotFound => "指定的文件不存在或已被删除",
475 Self::FileSizeExceeded => "文件大小超出限制,请压缩后重试",
476 Self::FileTypeNotSupported => "不支持的文件类型",
477 Self::CalendarNotFound => "指定的日历不存在",
478 Self::EventNotFound => "指定的日程不存在或已被删除",
479 Self::EventConflict => "日程时间冲突,请选择其他时间",
480 Self::DocumentNotFound => "指定的文档不存在或已被删除",
481 Self::DocumentPermissionDenied => "文档权限不足,请联系文档所有者授权",
482 Self::DocumentLocked => "文档已被其他用户锁定,请稍后再试",
483 Self::SheetNotFound => "指定的工作表不存在",
484 Self::TableNotFound => "指定的表格不存在",
485 Self::AccessTokenFormatInvalid => "访问令牌格式或内容无效,请重新获取或检查传参",
486 Self::AccessTokenInvalid => "用户访问令牌无效或失效,需要重新获取用户授权",
487 Self::AppAccessTokenInvalid => "应用访问令牌无效,请检查应用ID和密钥配置",
488 Self::TenantAccessTokenInvalid => "租户访问令牌无效,请检查应用是否正确安装到企业",
489 Self::SsoTokenInvalid => "SSO 访问令牌无效,请重新登录或检查 SSO 配置",
490 Self::PermissionMissing => "缺少所需权限,请在开发者后台开通对应权限范围",
491 Self::AccessTokenNoPermission => "访问令牌权限不足,请重新授权所需 scope",
492 Self::AccessTokenExpiredV2 => "访问令牌已过期,需要重新获取",
493 Self::UserSessionInvalid => "用户会话失效,请重新登录或刷新会话",
494 Self::UserSessionNotFound => "未找到有效用户会话,请重新登录",
495 Self::UserSessionTimeout => "用户会话已超时,请重新登录",
496 Self::UserIdentityInvalid => "用户身份解析失败,请检查传入的身份标识",
497 Self::UserTypeNotSupportedV2 => "当前用户类型不支持该操作",
498 Self::UserIdentityMismatch => "用户身份不匹配,请检查 user_id/open_id 组合",
499 Self::UserIdInvalid => "用户ID非法,请检查参数格式",
500 Self::OpenIdInvalid => "OpenID 非法,请检查参数格式",
501 Self::UnionIdInvalid => "UnionID 非法,请检查参数格式",
502
503 Self::NetworkTimeout => "网络请求超时,请检查网络连接或增加超时时间",
504 Self::NetworkConnectionFailed => "网络连接失败,请检查网络设置和服务器状态",
505 Self::DnsResolutionFailed => "DNS解析失败,请检查域名设置或网络配置",
506 Self::SslCertificateError => "SSL证书验证失败,请检查证书配置或系统时间",
507 Self::ConnectionRefused => "连接被拒绝,请检查服务器状态和防火墙设置",
508
509 Self::SerializationError => "JSON序列化或反序列化失败,请检查数据格式",
510 Self::DataFormatError => "数据格式不正确,请检查输入数据结构",
511 Self::EncodingError => "数据编码错误,请检查字符编码设置",
512
513 Self::AuthenticationFailed => "身份验证失败,请检查凭据是否正确",
514 Self::PermissionDenied => "权限不足,无法执行此操作",
515 Self::TokenExpired => "访问令牌已过期,需要重新获取",
516 Self::InvalidSignature => "签名验证失败,请检查签名算法和密钥",
517
518 Self::ValidationError => "输入数据验证失败,请检查数据格式和内容",
519 Self::MissingRequiredParameter => "缺少必需的参数,请检查请求参数完整性",
520 Self::InvalidParameterFormat => "参数格式不正确,请按照要求格式提供参数",
521 Self::ParameterOutOfRange => "参数值超出允许范围,请检查参数值是否有效",
522
523 Self::BusinessError => "业务逻辑处理失败,请检查操作条件和业务规则",
524 Self::OperationNotSupported => "当前操作不被支持,请检查API文档或使用替代方法",
525 Self::ResourceConflict => "资源状态冲突,请检查资源当前状态或等待后重试",
526
527 Self::InternalError => "系统内部错误,请联系技术支持",
528 Self::ConfigurationError => "系统配置错误,请检查配置文件或环境变量",
529 Self::ResourceExhausted => "系统资源耗尽,请稍后重试或增加资源配额",
530
531 Self::RateLimitExceeded => "请求频率超过限制,请降低请求频率后重试",
532
533 Self::ResponseTooLarge => "响应体大小超过限制,请减小请求范围或联系管理员调整限制",
534
535 Self::CacheMiss => "缓存中未找到数据,将从数据源获取",
536 Self::CacheServiceUnavailable => "缓存服务不可用,将直接访问数据源",
537
538 Self::SecurityPolicyViolation => "操作违反安全策略,请检查操作权限和安全设置",
539 Self::AccessDenied => "访问被拒绝,请检查访问权限和身份验证",
540
541 Self::Unknown => "未知错误,请联系技术支持获取帮助",
542 }
543 }
544
545 pub fn is_success(&self) -> bool {
547 matches!(self, Self::Success)
548 }
549
550 pub fn is_client_error(&self) -> bool {
552 let code = *self as i32;
553 (400..=499).contains(&code) && code != 429
554 }
555
556 pub fn is_server_error(&self) -> bool {
558 let code = *self as i32;
559 (500..=599).contains(&code)
560 }
561
562 pub fn is_network_error(&self) -> bool {
564 matches!(
565 self,
566 Self::NetworkTimeout
567 | Self::NetworkConnectionFailed
568 | Self::DnsResolutionFailed
569 | Self::SslCertificateError
570 | Self::ConnectionRefused
571 )
572 }
573
574 pub fn is_auth_error(&self) -> bool {
576 matches!(
577 self,
578 Self::Unauthorized
579 | Self::AccessTokenFormatInvalid
580 | Self::AccessTokenInvalid
581 | Self::AppAccessTokenInvalid
582 | Self::TenantAccessTokenInvalid
583 | Self::SsoTokenInvalid
584 | Self::AuthenticationFailed
585 | Self::TokenExpired
586 | Self::AccessTokenExpiredV2
587 | Self::InvalidSignature
588 | Self::UserSessionInvalid
589 | Self::UserSessionNotFound
590 | Self::UserSessionTimeout
591 | Self::UserIdentityInvalid
592 | Self::UserTypeNotSupportedV2
593 | Self::UserIdentityMismatch
594 | Self::UserIdInvalid
595 | Self::OpenIdInvalid
596 | Self::UnionIdInvalid
597 )
598 }
599
600 pub fn is_permission_error(&self) -> bool {
602 matches!(
603 self,
604 Self::Forbidden
605 | Self::AppPermissionDenied
606 | Self::DocumentPermissionDenied
607 | Self::PermissionDenied
608 | Self::PermissionMissing
609 | Self::AccessTokenNoPermission
610 | Self::AccessDenied
611 )
612 }
613
614 pub fn is_retryable(&self) -> bool {
616 match self {
617 Self::Success => false,
618 Self::TooManyRequests => true,
619 Self::InternalServerError => true,
620 Self::BadGateway => true,
621 Self::ServiceUnavailable => true,
622 Self::GatewayTimeout => true,
623 Self::NetworkTimeout => true,
624 Self::NetworkConnectionFailed => true,
625 Self::ConnectionRefused => true,
626 Self::CacheServiceUnavailable => true,
627 Self::RateLimitExceeded => true,
628 _ => false,
629 }
630 }
631
632 pub fn suggested_retry_delay(&self) -> Option<u64> {
634 match self {
635 Self::TooManyRequests => Some(60),
636 Self::InternalServerError => Some(5),
637 Self::BadGateway => Some(3),
638 Self::ServiceUnavailable => Some(10),
639 Self::GatewayTimeout => Some(5),
640 Self::NetworkTimeout => Some(3),
641 Self::NetworkConnectionFailed => Some(5),
642 Self::ConnectionRefused => Some(10),
643 Self::RateLimitExceeded => Some(30),
644 _ => None,
645 }
646 }
647
648 pub fn http_status(&self) -> Option<u16> {
650 match self {
651 Self::ResponseTooLarge => Some(413),
652 _ => {
653 let code = *self as i32;
654 if (100..=599).contains(&code) {
655 Some(code as u16)
656 } else {
657 None
658 }
659 }
660 }
661 }
662
663 pub fn from_http_status(status: u16) -> Self {
665 Self::from_code(status as i32)
666 }
667
668 pub fn from_feishu_code(code: i32) -> Option<Self> {
670 match code {
671 99991661 => Some(Self::AccessTokenFormatInvalid),
672 99991663 => Some(Self::TenantAccessTokenInvalid),
673 99991664 => Some(Self::AppAccessTokenInvalid),
674 99991670 => Some(Self::SsoTokenInvalid),
675 99991671 => Some(Self::AccessTokenInvalid),
676 99991672 => Some(Self::PermissionMissing),
677 99991676 => Some(Self::AccessTokenNoPermission),
678 99991677 => Some(Self::AccessTokenExpiredV2),
679 99991641 => Some(Self::UserSessionInvalid),
680 99991642 => Some(Self::UserSessionNotFound),
681 99991645 => Some(Self::UserSessionTimeout),
682 99991669 => Some(Self::UserIdentityInvalid),
683 99991674 => Some(Self::UserTypeNotSupportedV2),
684 99991675 => Some(Self::UserIdentityMismatch),
685 99992351 => Some(Self::UserIdInvalid),
686 99992352 => Some(Self::OpenIdInvalid),
687 99992353 => Some(Self::UnionIdInvalid),
688 _ => None,
689 }
690 }
691
692 pub fn for_error_type(error_type: &str) -> Self {
698 match error_type {
699 "Network" | "network" => Self::NetworkConnectionFailed,
701
702 "Authentication" | "authentication" => Self::AuthenticationFailed,
704 "TokenExpired" | "token_expired" => Self::TokenExpired,
705
706 "Permission" | "permission" => Self::PermissionDenied,
708 "Forbidden" | "forbidden" => Self::Forbidden,
709
710 "Validation" | "validation" => Self::ValidationError,
712 "ApiError" | "api_error" => Self::BusinessError,
713
714 "Configuration" | "configuration" => Self::ConfigurationError,
716
717 "Serialization" | "serialization" => Self::SerializationError,
719
720 "Internal" | "internal" => Self::InternalError,
722 "Unknown" | "unknown" => Self::Unknown,
723
724 "BadRequest" | "bad_request" => Self::BadRequest,
726 "Unauthorized" | "unauthorized" => Self::Unauthorized,
727 "NotFound" | "not_found" => Self::NotFound,
728 "Conflict" | "conflict" => Self::Conflict,
729 "TooManyRequests" | "too_many_requests" => Self::TooManyRequests,
730 "InternalServerError" | "internal_server_error" => Self::InternalServerError,
731 "ServiceUnavailable" | "service_unavailable" => Self::ServiceUnavailable,
732
733 _ => Self::Unknown,
735 }
736 }
737
738 pub fn network() -> Self {
740 Self::NetworkConnectionFailed
741 }
742
743 pub fn authentication() -> Self {
745 Self::AuthenticationFailed
746 }
747
748 pub fn permission() -> Self {
750 Self::PermissionDenied
751 }
752
753 pub fn validation() -> Self {
755 Self::ValidationError
756 }
757
758 pub fn configuration() -> Self {
760 Self::ConfigurationError
761 }
762
763 pub fn internal() -> Self {
765 Self::InternalError
766 }
767
768 pub fn serialization() -> Self {
770 Self::SerializationError
771 }
772
773 pub fn unknown() -> Self {
775 Self::Unknown
776 }
777
778 pub fn from_message(message: &str) -> Self {
782 let msg_lower = message.to_lowercase();
783
784 if msg_lower.contains("network")
786 || msg_lower.contains("connection")
787 || msg_lower.contains("timeout")
788 || msg_lower.contains("dns")
789 {
790 return Self::NetworkConnectionFailed;
791 }
792
793 if msg_lower.contains("auth")
795 || msg_lower.contains("token")
796 || msg_lower.contains("unauthorized")
797 {
798 return Self::AuthenticationFailed;
799 }
800
801 if msg_lower.contains("permission")
803 || msg_lower.contains("forbidden")
804 || msg_lower.contains("access denied")
805 {
806 return Self::PermissionDenied;
807 }
808
809 if msg_lower.contains("invalid")
811 || msg_lower.contains("validation")
812 || msg_lower.contains("parameter")
813 {
814 return Self::ValidationError;
815 }
816
817 if msg_lower.contains("config") || msg_lower.contains("setting") {
819 return Self::ConfigurationError;
820 }
821
822 if msg_lower.contains("json")
824 || msg_lower.contains("serialize")
825 || msg_lower.contains("parse")
826 {
827 return Self::SerializationError;
828 }
829
830 if msg_lower.contains("not found") || msg_lower.contains("missing") {
832 return Self::NotFound;
833 }
834
835 if msg_lower.contains("unavailable") || msg_lower.contains("service") {
837 return Self::ServiceUnavailable;
838 }
839
840 Self::Unknown
841 }
842
843 pub fn should_retry(&self, attempt: u32) -> bool {
847 if !self.is_retryable() {
848 return false;
849 }
850
851 match self {
852 Self::TooManyRequests | Self::RateLimitExceeded => attempt < 5,
854 Self::NetworkTimeout | Self::NetworkConnectionFailed => attempt < 3,
855 Self::InternalServerError | Self::BadGateway | Self::GatewayTimeout => attempt < 2,
856 _ => attempt < 3,
857 }
858 }
859
860 pub fn exponential_backoff_delay(&self, attempt: u32) -> Option<u64> {
864 if !self.should_retry(attempt) {
865 return None;
866 }
867
868 let base_delay = self.suggested_retry_delay().unwrap_or(1);
869 let max_delay = match self {
870 Self::TooManyRequests => 300, Self::RateLimitExceeded => 180, _ => 60, };
874
875 let delay = base_delay * 2u64.pow(attempt.saturating_sub(1));
877 Some(delay.min(max_delay))
878 }
879
880 pub fn severity(&self) -> ErrorSeverity {
882 match self {
883 Self::Success => ErrorSeverity::Info,
884
885 Self::BadRequest
887 | Self::Forbidden
888 | Self::NotFound
889 | Self::MethodNotAllowed
890 | Self::Conflict
891 | Self::ValidationError
892 | Self::MissingRequiredParameter
893 | Self::InvalidParameterFormat
894 | Self::ParameterOutOfRange
895 | Self::ResponseTooLarge => ErrorSeverity::Warning,
896
897 Self::Unauthorized
899 | Self::AccessTokenInvalid
900 | Self::AppAccessTokenInvalid
901 | Self::TenantAccessTokenInvalid
902 | Self::AuthenticationFailed
903 | Self::TokenExpired
904 | Self::PermissionDenied
905 | Self::AccessDenied => ErrorSeverity::Error,
906
907 Self::InternalServerError
909 | Self::BadGateway
910 | Self::ServiceUnavailable
911 | Self::GatewayTimeout
912 | Self::NetworkTimeout
913 | Self::NetworkConnectionFailed
914 | Self::DnsResolutionFailed
915 | Self::SslCertificateError
916 | Self::ConnectionRefused
917 | Self::InternalError
918 | Self::ConfigurationError
919 | Self::ResourceExhausted => ErrorSeverity::Critical,
920
921 _ => ErrorSeverity::Error,
923 }
924 }
925
926 pub fn recovery_suggestion(&self) -> &'static str {
928 match self {
929 Self::BadRequest => "请检查请求参数是否正确",
930 Self::Unauthorized => "请重新进行身份验证",
931 Self::Forbidden => "请联系管理员获取相应权限",
932 Self::NotFound => "请确认资源是否存在",
933 Self::TooManyRequests => "请降低请求频率后重试",
934 Self::NetworkConnectionFailed => "请检查网络连接",
935 Self::TokenExpired => "请重新获取访问令牌",
936 Self::ServiceUnavailable => "服务暂时不可用,请稍后重试",
937 Self::InternalServerError => "系统内部错误,请联系技术支持",
938 Self::ValidationError => "请检查输入参数格式",
939 Self::ConfigurationError => "请检查系统配置",
940 Self::ResponseTooLarge => "请减小请求范围或联系管理员调整响应大小限制",
941 Self::Unknown => "发生未知错误,请联系技术支持",
942 _ => "请稍后重试,如问题持续请联系技术支持",
943 }
944 }
945
946 pub fn is_user_error(&self) -> bool {
948 matches!(
949 self,
950 Self::BadRequest
951 | Self::Unauthorized
952 | Self::Forbidden
953 | Self::ValidationError
954 | Self::MissingRequiredParameter
955 | Self::InvalidParameterFormat
956 | Self::ParameterOutOfRange
957 | Self::AccessTokenInvalid
958 | Self::TokenExpired
959 )
960 }
961
962 pub fn is_system_error(&self) -> bool {
964 matches!(
965 self,
966 Self::InternalServerError
967 | Self::BadGateway
968 | Self::ServiceUnavailable
969 | Self::GatewayTimeout
970 | Self::NetworkTimeout
971 | Self::NetworkConnectionFailed
972 | Self::DnsResolutionFailed
973 | Self::SslCertificateError
974 | Self::ConnectionRefused
975 | Self::InternalError
976 | Self::ConfigurationError
977 | Self::ResourceExhausted
978 )
979 }
980
981 pub fn log_level(&self) -> &'static str {
983 match self.severity() {
984 ErrorSeverity::Info => "info",
985 ErrorSeverity::Warning => "warn",
986 ErrorSeverity::Error => "error",
987 ErrorSeverity::Critical => "error",
988 }
989 }
990}
991
992impl fmt::Display for ErrorCode {
993 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
994 write!(f, "{} ({})", self.description(), self.as_code())
995 }
996}
997
998#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1000pub enum ErrorCategory {
1001 Success,
1003 Authentication,
1005 Permission,
1007 Parameter,
1009 Resource,
1011 Server,
1013 Network,
1015 Business,
1017 System,
1019 RateLimit,
1021 Other,
1023}
1024
1025impl ErrorCode {
1026 pub fn category(&self) -> ErrorCategory {
1028 match self {
1029 Self::Success => ErrorCategory::Success,
1030
1031 Self::Unauthorized
1033 | Self::AppTicketInvalid
1034 | Self::AccessTokenFormatInvalid
1035 | Self::AccessTokenInvalid
1036 | Self::AppAccessTokenInvalid
1037 | Self::TenantAccessTokenInvalid
1038 | Self::SsoTokenInvalid
1039 | Self::AuthenticationFailed
1040 | Self::TokenExpired
1041 | Self::AccessTokenExpiredV2
1042 | Self::InvalidSignature
1043 | Self::UserSessionInvalid
1044 | Self::UserSessionNotFound
1045 | Self::UserSessionTimeout
1046 | Self::UserIdentityInvalid
1047 | Self::UserTypeNotSupportedV2
1048 | Self::UserIdentityMismatch
1049 | Self::UserIdInvalid
1050 | Self::OpenIdInvalid
1051 | Self::UnionIdInvalid => ErrorCategory::Authentication,
1052
1053 Self::Forbidden
1055 | Self::AppPermissionDenied
1056 | Self::DocumentPermissionDenied
1057 | Self::PermissionDenied
1058 | Self::PermissionMissing
1059 | Self::AccessTokenNoPermission
1060 | Self::AccessDenied
1061 | Self::SecurityPolicyViolation => ErrorCategory::Permission,
1062
1063 Self::BadRequest
1065 | Self::ValidationError
1066 | Self::MissingRequiredParameter
1067 | Self::InvalidParameterFormat
1068 | Self::ParameterOutOfRange
1069 | Self::ChatTypeNotSupported
1070 | Self::MessageTypeNotSupported
1071 | Self::FileTypeNotSupported => ErrorCategory::Parameter,
1072
1073 Self::NotFound
1075 | Self::AppNotInstalled
1076 | Self::UserNotFound
1077 | Self::UserStatusException
1078 | Self::DepartmentNotFound
1079 | Self::ChatNotFound
1080 | Self::MessageNotFound
1081 | Self::FileNotFound
1082 | Self::FileSizeExceeded
1083 | Self::CalendarNotFound
1084 | Self::EventNotFound
1085 | Self::DocumentNotFound
1086 | Self::DocumentLocked
1087 | Self::SheetNotFound
1088 | Self::TableNotFound => ErrorCategory::Resource,
1089
1090 Self::InternalServerError
1092 | Self::BadGateway
1093 | Self::ServiceUnavailable
1094 | Self::GatewayTimeout
1095 | Self::AppStatusException
1096 | Self::InternalError
1097 | Self::CacheServiceUnavailable => ErrorCategory::Server,
1098
1099 Self::NetworkTimeout
1101 | Self::NetworkConnectionFailed
1102 | Self::DnsResolutionFailed
1103 | Self::SslCertificateError
1104 | Self::ConnectionRefused => ErrorCategory::Network,
1105
1106 Self::Conflict
1108 | Self::EventConflict
1109 | Self::BusinessError
1110 | Self::OperationNotSupported
1111 | Self::ResourceConflict => ErrorCategory::Business,
1112
1113 Self::MethodNotAllowed
1115 | Self::SerializationError
1116 | Self::DataFormatError
1117 | Self::EncodingError
1118 | Self::ConfigurationError
1119 | Self::ResourceExhausted
1120 | Self::ResponseTooLarge => ErrorCategory::System,
1121
1122 Self::TooManyRequests | Self::RateLimitExceeded => ErrorCategory::RateLimit,
1124
1125 _ => ErrorCategory::Other,
1127 }
1128 }
1129}
1130
1131#[cfg(test)]
1132mod tests {
1133 use super::*;
1134
1135 #[test]
1136 fn test_error_code_conversion() {
1137 assert_eq!(ErrorCode::from_code(0), ErrorCode::Success);
1138 assert_eq!(ErrorCode::from_code(404), ErrorCode::NotFound);
1139 assert_eq!(ErrorCode::from_code(999999), ErrorCode::Unknown);
1140 }
1141
1142 #[test]
1143 fn test_error_code_properties() {
1144 let not_found = ErrorCode::NotFound;
1145 assert_eq!(not_found.as_code(), 404);
1146 assert_eq!(not_found.description(), "资源不存在");
1147 assert!(not_found.is_client_error());
1148 assert!(!not_found.is_server_error());
1149 assert!(!not_found.is_retryable());
1150
1151 let timeout = ErrorCode::NetworkTimeout;
1152 assert!(timeout.is_network_error());
1153 assert!(timeout.is_retryable());
1154 assert_eq!(timeout.suggested_retry_delay(), Some(3));
1155 }
1156
1157 #[test]
1158 fn test_http_status_conversion() {
1159 assert_eq!(ErrorCode::from_http_status(200), ErrorCode::Success);
1160 assert_eq!(ErrorCode::from_http_status(404), ErrorCode::NotFound);
1161 assert_eq!(
1162 ErrorCode::from_http_status(500),
1163 ErrorCode::InternalServerError
1164 );
1165 }
1166
1167 #[test]
1168 fn test_error_categories() {
1169 let auth_error = ErrorCode::Unauthorized;
1170 assert_eq!(auth_error.category(), ErrorCategory::Authentication);
1171
1172 let perm_error = ErrorCode::Forbidden;
1173 assert_eq!(perm_error.category(), ErrorCategory::Permission);
1174
1175 let net_error = ErrorCode::NetworkTimeout;
1176 assert_eq!(net_error.category(), ErrorCategory::Network);
1177 }
1178
1179 #[test]
1180 fn test_error_code_display() {
1181 let error = ErrorCode::AccessTokenInvalid;
1182 let display = format!("{error}");
1183 assert!(display.contains("访问令牌无效"));
1184 assert!(display.contains("99991671"));
1185 }
1186
1187 #[test]
1188 fn test_new_error_code_methods() {
1189 assert_eq!(ErrorCode::network(), ErrorCode::NetworkConnectionFailed);
1191 assert_eq!(ErrorCode::authentication(), ErrorCode::AuthenticationFailed);
1192 assert_eq!(ErrorCode::permission(), ErrorCode::PermissionDenied);
1193 assert_eq!(ErrorCode::validation(), ErrorCode::ValidationError);
1194 assert_eq!(ErrorCode::configuration(), ErrorCode::ConfigurationError);
1195 assert_eq!(ErrorCode::internal(), ErrorCode::InternalError);
1196 assert_eq!(ErrorCode::serialization(), ErrorCode::SerializationError);
1197 assert_eq!(ErrorCode::unknown(), ErrorCode::Unknown);
1198 }
1199
1200 #[test]
1201 fn test_for_error_type() {
1202 assert_eq!(
1203 ErrorCode::for_error_type("Network"),
1204 ErrorCode::NetworkConnectionFailed
1205 );
1206 assert_eq!(
1207 ErrorCode::for_error_type("authentication"),
1208 ErrorCode::AuthenticationFailed
1209 );
1210 assert_eq!(
1211 ErrorCode::for_error_type("Validation"),
1212 ErrorCode::ValidationError
1213 );
1214 assert_eq!(
1215 ErrorCode::for_error_type("unknown_type"),
1216 ErrorCode::Unknown
1217 );
1218 }
1219
1220 #[test]
1221 fn test_from_message() {
1222 assert_eq!(
1223 ErrorCode::from_message("Network connection failed"),
1224 ErrorCode::NetworkConnectionFailed
1225 );
1226 assert_eq!(
1227 ErrorCode::from_message("Authentication token invalid"),
1228 ErrorCode::AuthenticationFailed
1229 );
1230 assert_eq!(
1231 ErrorCode::from_message("Permission denied"),
1232 ErrorCode::PermissionDenied
1233 );
1234 assert_eq!(
1235 ErrorCode::from_message("Invalid parameter"),
1236 ErrorCode::ValidationError
1237 );
1238 assert_eq!(
1239 ErrorCode::from_message("JSON parse error"),
1240 ErrorCode::SerializationError
1241 );
1242 assert_eq!(
1243 ErrorCode::from_message("Unknown error occurred"),
1244 ErrorCode::Unknown
1245 );
1246 }
1247
1248 #[test]
1249 fn test_retry_logic() {
1250 let retryable_error = ErrorCode::NetworkTimeout;
1251 assert!(retryable_error.should_retry(0));
1252 assert!(retryable_error.should_retry(1));
1253 assert!(!retryable_error.should_retry(3)); let non_retryable_error = ErrorCode::BadRequest;
1256 assert!(!non_retryable_error.should_retry(0));
1257
1258 let delay = retryable_error.exponential_backoff_delay(2);
1260 assert!(delay.is_some());
1261 assert!(delay.unwrap() > 1); }
1263
1264 #[test]
1265 fn test_error_severity() {
1266 assert_eq!(ErrorCode::Success.severity(), ErrorSeverity::Info);
1267 assert_eq!(ErrorCode::BadRequest.severity(), ErrorSeverity::Warning);
1268 assert_eq!(ErrorCode::Unauthorized.severity(), ErrorSeverity::Error);
1269 assert_eq!(
1270 ErrorCode::InternalServerError.severity(),
1271 ErrorSeverity::Critical
1272 );
1273 }
1274
1275 #[test]
1276 fn test_recovery_suggestions() {
1277 let suggestion = ErrorCode::NetworkConnectionFailed.recovery_suggestion();
1278 assert!(suggestion.contains("网络"));
1279
1280 let suggestion = ErrorCode::TokenExpired.recovery_suggestion();
1281 assert!(suggestion.contains("令牌"));
1282
1283 let suggestion = ErrorCode::ValidationError.recovery_suggestion();
1284 assert!(suggestion.contains("参数"));
1285 }
1286
1287 #[test]
1288 fn test_user_vs_system_errors() {
1289 assert!(ErrorCode::BadRequest.is_user_error());
1291 assert!(ErrorCode::Unauthorized.is_user_error());
1292 assert!(ErrorCode::ValidationError.is_user_error());
1293
1294 assert!(ErrorCode::InternalServerError.is_system_error());
1296 assert!(ErrorCode::NetworkTimeout.is_system_error());
1297 assert!(ErrorCode::ServiceUnavailable.is_system_error());
1298
1299 assert!(!ErrorCode::BadRequest.is_system_error());
1301 assert!(!ErrorCode::InternalServerError.is_user_error());
1302 }
1303
1304 #[test]
1305 fn test_log_levels() {
1306 assert_eq!(ErrorCode::Success.log_level(), "info");
1307 assert_eq!(ErrorCode::BadRequest.log_level(), "warn");
1308 assert_eq!(ErrorCode::Unauthorized.log_level(), "error");
1309 assert_eq!(ErrorCode::InternalServerError.log_level(), "error");
1310 }
1311}
1312
1313#[cfg(test)]
1314mod severity_tests {
1315 use super::*;
1316
1317 #[test]
1318 fn test_error_severity_basic() {
1319 assert_eq!(ErrorSeverity::Info.as_level(), 0);
1320 assert_eq!(ErrorSeverity::Warning.as_level(), 1);
1321 assert_eq!(ErrorSeverity::Error.as_level(), 2);
1322 assert_eq!(ErrorSeverity::Critical.as_level(), 3);
1323 }
1324
1325 #[test]
1326 fn test_error_severity_ordering() {
1327 assert!(ErrorSeverity::Info < ErrorSeverity::Warning);
1328 assert!(ErrorSeverity::Warning < ErrorSeverity::Error);
1329 assert!(ErrorSeverity::Error < ErrorSeverity::Critical);
1330 }
1331
1332 #[test]
1333 fn test_error_severity_from_level() {
1334 assert_eq!(ErrorSeverity::from_level(0), ErrorSeverity::Info);
1335 assert_eq!(ErrorSeverity::from_level(1), ErrorSeverity::Warning);
1336 assert_eq!(ErrorSeverity::from_level(2), ErrorSeverity::Error);
1337 assert_eq!(ErrorSeverity::from_level(3), ErrorSeverity::Critical);
1338 assert_eq!(ErrorSeverity::from_level(99), ErrorSeverity::Error); }
1340
1341 #[test]
1342 fn test_error_severity_properties() {
1343 assert!(!ErrorSeverity::Info.requires_immediate_action());
1344 assert!(!ErrorSeverity::Warning.requires_immediate_action());
1345 assert!(ErrorSeverity::Error.requires_immediate_action());
1346 assert!(ErrorSeverity::Critical.requires_immediate_action());
1347
1348 assert!(!ErrorSeverity::Info.requires_user_intervention());
1349 assert!(!ErrorSeverity::Warning.requires_user_intervention());
1350 assert!(ErrorSeverity::Error.requires_user_intervention());
1351 assert!(ErrorSeverity::Critical.requires_user_intervention());
1352
1353 assert!(ErrorSeverity::Info.is_auto_recoverable());
1354 assert!(ErrorSeverity::Warning.is_auto_recoverable());
1355 assert!(!ErrorSeverity::Error.is_auto_recoverable());
1356 assert!(!ErrorSeverity::Critical.is_auto_recoverable());
1357 }
1358
1359 #[test]
1360 fn test_error_severity_display() {
1361 assert_eq!(format!("{}", ErrorSeverity::Info), "信息");
1362 assert_eq!(format!("{}", ErrorSeverity::Warning), "警告");
1363 assert_eq!(format!("{}", ErrorSeverity::Error), "错误");
1364 assert_eq!(format!("{}", ErrorSeverity::Critical), "严重错误");
1365 }
1366}