parent
c94912252a
commit
28c3e96966
7 changed files with 150 additions and 235 deletions
@ -1,95 +1,39 @@ |
|||||||
use std::fmt; |
|
||||||
use std::error::Error as StdError; |
|
||||||
use reqwest::Error as ReqError; |
use reqwest::Error as ReqError; |
||||||
use serde::{ |
use serde::{ |
||||||
Serialize, |
Serialize, |
||||||
Deserialize |
Deserialize |
||||||
}; |
}; |
||||||
|
use thiserror::Error; |
||||||
|
use uuid::Uuid; |
||||||
|
|
||||||
//pub type BoxError = Box<(dyn StdError + Send + Sync + 'static)>;
|
|
||||||
pub type UResult<T> = std::result::Result<T, UError>; |
pub type UResult<T> = std::result::Result<T, UError>; |
||||||
|
|
||||||
|
#[derive(Error, Debug, Serialize, Deserialize, Clone)] |
||||||
|
pub enum UError { |
||||||
|
#[error("Error: {0}")] |
||||||
|
Raw(&'static str), |
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)] |
#[error("Connection error: {0}")] |
||||||
pub enum JobErrType { |
ConnectionError(String), |
||||||
AlreadyRunning, |
|
||||||
Finished, |
|
||||||
System |
|
||||||
} |
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)] |
#[error("Parse error")] |
||||||
pub enum UErrType { |
|
||||||
ConnectionError, |
|
||||||
ParseError, |
ParseError, |
||||||
JobError(JobErrType), |
|
||||||
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 |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
pub fn new_type(err_type: UErrType) -> Self { |
#[error("Job error: {0}")] |
||||||
UError::new(err_type, String::new()) |
JobError(String), |
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
impl fmt::Debug for UError { |
#[error("Job is uncompleted yet")] |
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
JobUncompleted, |
||||||
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 { |
#[error("Job cannot be ran on this platform. Expected: {0}, got: {1}")] |
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
InsuitablePlatform(String, String), |
||||||
let e_type = match self.inner.err_type { |
|
||||||
UErrType::Raw(ref msg) => msg.clone(), |
|
||||||
UErrType::ConnectionError => "Connection error".to_string(), |
|
||||||
UErrType::ParseError => "Parse error".to_string(), |
|
||||||
UErrType::JobError(ref inner) => |
|
||||||
(String::from("Job error: ") + match *inner { |
|
||||||
JobErrType::AlreadyRunning => "job is already running", |
|
||||||
JobErrType::Finished => "once-scheduled job is already finished", |
|
||||||
JobErrType::System => "system error" |
|
||||||
}), |
|
||||||
UErrType::Unknown => "Unknown error".to_string(), |
|
||||||
}; |
|
||||||
f.write_str(&e_type)?; |
|
||||||
|
|
||||||
write!(f, ": {}", self.inner.source) |
#[error("Task {0} doesn't exist")] |
||||||
} |
NoTask(Uuid) |
||||||
} |
} |
||||||
|
|
||||||
impl From<ReqError> for UError { |
impl From<ReqError> for UError { |
||||||
fn from(e: ReqError) -> Self { |
fn from(e: ReqError) -> Self { |
||||||
let err_type = if e.is_request() { |
UError::ConnectionError(e.to_string()) |
||||||
UErrType::ConnectionError |
|
||||||
} else if e.is_decode() { |
|
||||||
UErrType::ParseError |
|
||||||
} else { |
|
||||||
UErrType::Unknown |
|
||||||
}; |
|
||||||
UError::new(err_type, e.to_string()) |
|
||||||
} |
} |
||||||
} |
} |
Loading…
Reference in new issue