You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

79 lines
1.9 KiB

use std::fmt;
use std::error::Error as StdError;
use reqwest::Error as ReqError;
use serde::{
Serialize,
Deserialize
};
pub type BoxError = Box<(dyn StdError + Send + Sync + 'static)>;
pub type UResult<T> = std::result::Result<T, UError>;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum UErrType {
ConnectionError,
ParseError,
JobError,
Unknown,
Raw(String)
}
#[derive(Serialize, Deserialize, Clone, Debug)]
struct Inner {
err_type: UErrType,
source: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct UError {
inner: Box<Inner>
}
impl UError {
pub fn new(err_type: UErrType, source: String) -> Self {
Self {
inner: Box::new(Inner {
source,
err_type
})
}
}
}
impl fmt::Debug for UError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("errors::UError");
builder.field("kind", &self.inner.err_type);
builder.field("source", &self.inner.source);
builder.finish()
}
}
impl fmt::Display for UError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let e_type = match self.inner.err_type {
UErrType::Raw(ref msg) => msg,
UErrType::ConnectionError => "Connection error",
UErrType::ParseError => "Parse error",
UErrType::JobError => "Job error",
UErrType::Unknown => "Unknown error",
};
f.write_str(e_type)?;
write!(f, ": {}", self.inner.source)
}
}
impl From<ReqError> for UError {
fn from(e: ReqError) -> Self {
let err_type = if e.is_request() {
UErrType::ConnectionError
} else if e.is_decode() {
UErrType::ParseError
} else {
UErrType::Unknown
};
UError::new(err_type, e.to_string())
}
}