use std::io::Error as IoError;
use std::num::ParseFloatError;
use std::num::ParseIntError;
use quick_xml::Error as XmlError;
use thiserror::Error;
use ureq::Error as UreqError;
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ApiError {
#[error("bad request: {0}")]
BadRequest(String),
#[error("not found: {0}")]
NotFound(String),
#[error("not allowed: {0}")]
NotAllowed(String),
#[error("timeout: {0}")]
Timeout(String),
#[error("server busy: {0}")]
ServerBusy(String),
#[error("unimplemented!(): {0}")]
Unimplemented(String),
#[error("server error: {0}")]
ServerError(String),
#[error("unknown error: {0}")]
Unknown(String),
}
impl From<crate::model::rest::Fault> for ApiError {
fn from(fault: crate::model::rest::Fault) -> Self {
match fault.code.as_str() {
"PUGREST.BadRequest" => ApiError::BadRequest(fault.message),
"PUGREST.NotFound" => ApiError::NotFound(fault.message),
"PUGREST.NotAllowed" => ApiError::NotAllowed(fault.message),
"PUGREST.Timeout" => ApiError::Timeout(fault.message),
"PUGREST.ServerBusy" => ApiError::ServerBusy(fault.message),
"PUGREST.Unimplemented" => ApiError::Unimplemented(fault.message),
"PUGREST.ServerError" => ApiError::ServerError(fault.message),
_ => ApiError::Unknown(fault.message),
}
}
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ParseError {
#[error(transparent)]
Int(#[from] ParseIntError),
#[error(transparent)]
Float(#[from] ParseFloatError),
}
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Api(#[from] ApiError),
#[error(transparent)]
Request(#[from] UreqError),
#[error(transparent)]
Xml(#[from] XmlError),
#[error(transparent)]
Parse(#[from] ParseError),
}
impl From<IoError> for Error {
fn from(e: IoError) -> Self {
Self::from(XmlError::Io(e))
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Self::Parse(ParseError::Int(e))
}
}
impl From<ParseFloatError> for Error {
fn from(e: ParseFloatError) -> Self {
Self::Parse(ParseError::Float(e))
}
}
pub type Result<T> = std::result::Result<T, Error>;