81 lines
2.3 KiB
Rust
81 lines
2.3 KiB
Rust
use super::api::ErrorModel;
|
|
use std::fmt::{Display, Formatter};
|
|
use std::io;
|
|
|
|
#[derive(Debug)]
|
|
pub enum LibError {
|
|
YepzonServerError(ErrorModel),
|
|
MinreqError(minreq::Error),
|
|
ChronoParseError(chrono::ParseError),
|
|
IOError(io::Error),
|
|
FromUTF8Error(std::string::FromUtf8Error),
|
|
MessagedError(String, Option<Box<LibError>>),
|
|
}
|
|
|
|
impl From<minreq::Error> for LibError {
|
|
fn from(error: minreq::Error) -> Self {
|
|
LibError::MinreqError(error)
|
|
}
|
|
}
|
|
|
|
impl From<chrono::ParseError> for LibError {
|
|
fn from(error: chrono::ParseError) -> Self {
|
|
LibError::ChronoParseError(error)
|
|
}
|
|
}
|
|
|
|
impl From<ErrorModel> for LibError {
|
|
fn from(error: ErrorModel) -> Self {
|
|
LibError::YepzonServerError(error)
|
|
}
|
|
}
|
|
|
|
impl From<io::Error> for LibError {
|
|
fn from(error: io::Error) -> Self {
|
|
LibError::IOError(error)
|
|
}
|
|
}
|
|
|
|
impl From<std::string::FromUtf8Error> for LibError {
|
|
fn from(error: std::string::FromUtf8Error) -> Self {
|
|
LibError::FromUTF8Error(error)
|
|
}
|
|
}
|
|
|
|
impl Display for LibError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
let err = match self {
|
|
LibError::ChronoParseError(e) => format!("Date-Time value parse error: {}", e),
|
|
LibError::FromUTF8Error(e) => format!("UTF-8 error: {}", e),
|
|
LibError::IOError(e) => format!("IO error: {}", e),
|
|
LibError::MinreqError(e) => format!("Network error: {}", e),
|
|
LibError::YepzonServerError(e) => format!(
|
|
"Yepzon server error: {}",
|
|
e.message.as_ref().unwrap_or(&String::new())
|
|
),
|
|
LibError::MessagedError(msg, err) => format!(
|
|
"{}\n {}",
|
|
msg,
|
|
err.as_ref().map(|e| e.to_string()).unwrap_or(String::new())
|
|
),
|
|
};
|
|
write!(f, "{}", err)
|
|
}
|
|
}
|
|
|
|
pub trait MessagedError<A> {
|
|
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, LibError>;
|
|
}
|
|
|
|
impl<A, B: Into<LibError>> MessagedError<A> for Result<A, B> {
|
|
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, LibError> {
|
|
match self {
|
|
Ok(ok) => Ok(ok),
|
|
Err(e) => Err(LibError::MessagedError(
|
|
text.into(),
|
|
Some(Box::new(e.into())),
|
|
)),
|
|
}
|
|
}
|
|
}
|