1use crate::constant::GlobalConfig;
6
7#[derive(Debug)]
11pub struct BaseException {
12 pub name: String,
14 pub status: u32,
16 pub message: String,
18}
19
20impl BaseException {
21 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#[derive(Debug)]
41pub struct CoolCommException {
42 inner: BaseException,
43}
44
45impl CoolCommException {
46 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 pub fn status(&self) -> u32 {
67 self.inner.status
68 }
69
70 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#[derive(Debug)]
88pub struct CoolCoreException {
89 inner: BaseException,
90}
91
92impl CoolCoreException {
93 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 pub fn status(&self) -> u32 {
114 self.inner.status
115 }
116
117 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#[derive(Debug)]
135pub struct CoolValidateException {
136 inner: BaseException,
137}
138
139impl CoolValidateException {
140 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 pub fn status(&self) -> u32 {
161 self.inner.status
162 }
163
164 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 {}