Skip to main content

cool_plugin/
exception.rs

1//! 插件异常处理
2//!
3//! 对应 TypeScript 版本的 `exception/*.ts`
4
5use crate::constant::GlobalConfig;
6
7/// 异常基类
8///
9/// 对应 TypeScript 版本的 `BaseException`
10#[derive(Debug)]
11pub struct BaseException {
12    /// 异常名称
13    pub name: String,
14    /// 状态码
15    pub status: u32,
16    /// 错误消息
17    pub message: String,
18}
19
20impl BaseException {
21    /// 创建新的异常
22    pub fn new(name: String, code: u32, message: String) -> Self {
23        Self {
24            name,
25            status: code,
26            message,
27        }
28    }
29}
30
31impl std::fmt::Display for BaseException {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{}: {}", self.name, self.message)
34    }
35}
36
37/// 通用异常
38///
39/// 对应 TypeScript 版本的 `CoolCommException`
40#[derive(Debug)]
41pub struct CoolCommException {
42    inner: BaseException,
43}
44
45impl CoolCommException {
46    /// 创建新的通用异常
47    pub fn new(message: impl Into<String>) -> Self {
48        let config = GlobalConfig::get_instance();
49        let message = message.into();
50        let msg = if message.is_empty() {
51            config.res_message().comm_fail().to_string()
52        } else {
53            message
54        };
55
56        Self {
57            inner: BaseException::new(
58                "CoolCommException".to_string(),
59                config.res_code().comm_fail(),
60                msg,
61            ),
62        }
63    }
64
65    /// 获取状态码
66    pub fn status(&self) -> u32 {
67        self.inner.status
68    }
69
70    /// 获取错误消息
71    pub fn message(&self) -> &str {
72        &self.inner.message
73    }
74}
75
76impl std::fmt::Display for CoolCommException {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        self.inner.fmt(f)
79    }
80}
81
82impl std::error::Error for CoolCommException {}
83
84/// 核心异常
85///
86/// 对应 TypeScript 版本的 `CoolCoreException`
87#[derive(Debug)]
88pub struct CoolCoreException {
89    inner: BaseException,
90}
91
92impl CoolCoreException {
93    /// 创建新的核心异常
94    pub fn new(message: impl Into<String>) -> Self {
95        let config = GlobalConfig::get_instance();
96        let message = message.into();
97        let msg = if message.is_empty() {
98            config.res_message().core_fail().to_string()
99        } else {
100            message
101        };
102
103        Self {
104            inner: BaseException::new(
105                "CoolCoreException".to_string(),
106                config.res_code().core_fail(),
107                msg,
108            ),
109        }
110    }
111
112    /// 获取状态码
113    pub fn status(&self) -> u32 {
114        self.inner.status
115    }
116
117    /// 获取错误消息
118    pub fn message(&self) -> &str {
119        &self.inner.message
120    }
121}
122
123impl std::fmt::Display for CoolCoreException {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        self.inner.fmt(f)
126    }
127}
128
129impl std::error::Error for CoolCoreException {}
130
131/// 验证异常
132///
133/// 对应 TypeScript 版本的 `CoolValidateException`
134#[derive(Debug)]
135pub struct CoolValidateException {
136    inner: BaseException,
137}
138
139impl CoolValidateException {
140    /// 创建新的验证异常
141    pub fn new(message: impl Into<String>) -> Self {
142        let config = GlobalConfig::get_instance();
143        let message = message.into();
144        let msg = if message.is_empty() {
145            config.res_message().validate_fail().to_string()
146        } else {
147            message
148        };
149
150        Self {
151            inner: BaseException::new(
152                "CoolValidateException".to_string(),
153                config.res_code().validate_fail(),
154                msg,
155            ),
156        }
157    }
158
159    /// 获取状态码
160    pub fn status(&self) -> u32 {
161        self.inner.status
162    }
163
164    /// 获取错误消息
165    pub fn message(&self) -> &str {
166        &self.inner.message
167    }
168}
169
170impl std::fmt::Display for CoolValidateException {
171    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172        self.inner.fmt(f)
173    }
174}
175
176impl std::error::Error for CoolValidateException {}