use crate::constant::GlobalConfig;
#[derive(Debug)]
pub struct BaseException {
pub name: String,
pub status: u32,
pub message: String,
}
impl BaseException {
pub fn new(name: String, code: u32, message: String) -> Self {
Self {
name,
status: code,
message,
}
}
}
impl std::fmt::Display for BaseException {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.name, self.message)
}
}
#[derive(Debug)]
pub struct CoolCommException {
inner: BaseException,
}
impl CoolCommException {
pub fn new(message: impl Into<String>) -> Self {
let config = GlobalConfig::get_instance();
let message = message.into();
let msg = if message.is_empty() {
config.res_message().comm_fail().to_string()
} else {
message
};
Self {
inner: BaseException::new(
"CoolCommException".to_string(),
config.res_code().comm_fail(),
msg,
),
}
}
pub fn status(&self) -> u32 {
self.inner.status
}
pub fn message(&self) -> &str {
&self.inner.message
}
}
impl std::fmt::Display for CoolCommException {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl std::error::Error for CoolCommException {}
#[derive(Debug)]
pub struct CoolCoreException {
inner: BaseException,
}
impl CoolCoreException {
pub fn new(message: impl Into<String>) -> Self {
let config = GlobalConfig::get_instance();
let message = message.into();
let msg = if message.is_empty() {
config.res_message().core_fail().to_string()
} else {
message
};
Self {
inner: BaseException::new(
"CoolCoreException".to_string(),
config.res_code().core_fail(),
msg,
),
}
}
pub fn status(&self) -> u32 {
self.inner.status
}
pub fn message(&self) -> &str {
&self.inner.message
}
}
impl std::fmt::Display for CoolCoreException {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl std::error::Error for CoolCoreException {}
#[derive(Debug)]
pub struct CoolValidateException {
inner: BaseException,
}
impl CoolValidateException {
pub fn new(message: impl Into<String>) -> Self {
let config = GlobalConfig::get_instance();
let message = message.into();
let msg = if message.is_empty() {
config.res_message().validate_fail().to_string()
} else {
message
};
Self {
inner: BaseException::new(
"CoolValidateException".to_string(),
config.res_code().validate_fail(),
msg,
),
}
}
pub fn status(&self) -> u32 {
self.inner.status
}
pub fn message(&self) -> &str {
&self.inner.message
}
}
impl std::fmt::Display for CoolValidateException {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl std::error::Error for CoolValidateException {}