79 lines
2.3 KiB
Rust
79 lines
2.3 KiB
Rust
use super::api::ErrorModel;
|
|
use std::fmt::{Display, Formatter};
|
|
use std::io;
|
|
|
|
#[derive(Debug)]
|
|
pub enum GenericError {
|
|
TomlError(toml::de::Error),
|
|
YepzonServerError(ErrorModel),
|
|
MinreqError(minreq::Error),
|
|
ChronoParseError(chrono::ParseError),
|
|
IOError(io::Error),
|
|
FromUTF8Error(std::string::FromUtf8Error),
|
|
MessagedError(String, Box<GenericError>),
|
|
}
|
|
|
|
impl From<toml::de::Error> for GenericError {
|
|
fn from(error: toml::de::Error) -> Self {
|
|
GenericError::TomlError(error)
|
|
}
|
|
}
|
|
|
|
impl From<minreq::Error> for GenericError {
|
|
fn from(error: minreq::Error) -> Self {
|
|
GenericError::MinreqError(error)
|
|
}
|
|
}
|
|
|
|
impl From<chrono::ParseError> for GenericError {
|
|
fn from(error: chrono::ParseError) -> Self {
|
|
GenericError::ChronoParseError(error)
|
|
}
|
|
}
|
|
|
|
impl From<ErrorModel> for GenericError {
|
|
fn from(error: ErrorModel) -> Self {
|
|
GenericError::YepzonServerError(error)
|
|
}
|
|
}
|
|
|
|
impl From<io::Error> for GenericError {
|
|
fn from(error: io::Error) -> Self {
|
|
GenericError::IOError(error)
|
|
}
|
|
}
|
|
|
|
impl From<std::string::FromUtf8Error> for GenericError {
|
|
fn from(error: std::string::FromUtf8Error) -> Self {
|
|
GenericError::FromUTF8Error(error)
|
|
}
|
|
}
|
|
|
|
impl Display for GenericError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
let err = match self {
|
|
GenericError::ChronoParseError(_) => "Chrono parse error".to_owned(),
|
|
GenericError::FromUTF8Error(_) => "UTF8 error".to_owned(),
|
|
GenericError::IOError(e) => format!("IO error: {}", e),
|
|
GenericError::MinreqError(_) => "Minreq error".to_owned(),
|
|
GenericError::TomlError(_) => "Toml error".to_owned(),
|
|
GenericError::YepzonServerError(_) => "Yepzon server error".to_owned(),
|
|
GenericError::MessagedError(msg, err) => format!("{}\n {}", msg, err),
|
|
};
|
|
write!(f, "{}", err)
|
|
}
|
|
}
|
|
|
|
pub trait MessagedError<A> {
|
|
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, GenericError>;
|
|
}
|
|
|
|
impl<A, B: Into<GenericError>> MessagedError<A> for Result<A, B> {
|
|
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, GenericError> {
|
|
match self {
|
|
Ok(ok) => Ok(ok),
|
|
Err(e) => Err(GenericError::MessagedError(text.into(), Box::new(e.into()))),
|
|
}
|
|
}
|
|
}
|