use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum Exception {
Io(std::io::Error), FileNotFound(String), DirectoryCreationFailed(String),
ParseError(String), InvalidFormat, MissingField(String),
ConnectionFailed(String), Timeout, InvalidUrl(String), ReqwestErr(reqwest::Error),
DataNotFound, DuplicateEntry(String), InvalidData(String),
PermissionDenied, Unauthorized,
ConfigError(String), MissingConfig(String),
Unknown(String), NotImplemented,
}
impl Display for Exception {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Exception::Io(e) => write!(f, "IO错误: {}", e),
Exception::FileNotFound(name) => write!(f, "文件未找到: {}", name),
Exception::ParseError(msg) => write!(f, "解析错误: {}", msg),
Exception::Unknown(msg) => write!(f, "未知错误: {}", msg),
_ => write!(f, "{:?}", self), }
}
}
impl std::error::Error for Exception { }
impl From<chrono::ParseError> for Exception {
fn from(err: chrono::ParseError) -> Self {
Exception::ParseError(format!("时间解析错误: {}", err))
}
}
impl From<std::io::Error> for Exception {
fn from(err: std::io::Error) -> Self {
Exception::Io(err)
}
}
impl From<serde_yaml::Error> for Exception {
fn from(err: serde_yaml::Error) -> Self {
Exception::ParseError(format!("YAML解析错误: {}", err))
}
}
impl From<reqwest::Error> for Exception {
fn from(err: reqwest::Error) -> Self {
Exception::ReqwestErr(err)
}
}
#[cfg(feature = "db-sqlite")]
impl From<sqlite::Error> for Exception {
fn from(value: sqlite::Error) -> Self {
Exception::Unknown(value.to_string())
}
}
#[cfg(feature = "db-mysql")]
impl From<mysql::Error> for Exception {
fn from(value: mysql::Error) -> Self {
match value {
_ => Exception::Unknown(value.to_string()),
}
}
}
#[cfg(feature = "db-mysql")]
impl From<mysql::UrlError> for Exception {
fn from(value: mysql::UrlError) -> Self {
match value {
_ => Exception::Unknown(value.to_string()),
}
}
}
impl From<url::ParseError> for Exception {
fn from(value: url::ParseError) -> Self {
Exception::InvalidUrl(value.to_string())
}
}
impl From<notify::Error> for Exception {
fn from(value: notify::Error) -> Self {
Exception::Unknown(value.to_string())
}
}