85 lines
2.0 KiB

mod chan;
pub use chan::*;
use crate::ufs;
use reqwest::Error as ReqError;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
pub type UResult<T> = std::result::Result<T, UError>;
#[derive(Error, Debug, Serialize, Deserialize, Clone)]
pub enum UError {
#[error("Runtime error: {0}")]
Runtime(String),
#[error("Connection error: {0}; url: {1}; body: '''{2}'''")]
NetError(String, String, String),
#[error("Parse error")]
ParseError,
#[error("Job error: {0}")]
JobError(String),
#[error("Job build failed: {0}")]
JobBuildError(String),
#[error("Job {0} doesn't exist")]
NoJob(Uuid),
#[error(transparent)]
FSError(#[from] ufs::Error),
#[error("Wrong auth token")]
WrongToken,
#[error("Panicked: {0}")]
Panic(String),
#[error("Panel error: {0}")]
PanelError(String),
#[error("Deserialize from json error: {0}, body: {1}")]
DeserializeError(String, String),
#[error("{0}\n{1}")]
Contexted(Box<UError>, String),
}
impl From<ReqError> for UError {
fn from(e: ReqError) -> Self {
UError::NetError(
e.to_string(),
e.url().map(|u| u.to_string()).unwrap_or_default(),
String::new(),
)
}
}
/*
impl From<serde_json::Error> for UError {
fn from(e: serde_json::Error) -> Self {
UError::DeserializeError(e.to_string())
}
}
*/
impl From<anyhow::Error> for UError {
fn from(e: anyhow::Error) -> Self {
let ctx = e
.chain()
.skip(1)
.map(|cause| format!("ctx: {}", cause))
.collect::<Vec<_>>()
.join("\n");
match e.downcast::<UError>() {
Ok(err) => UError::Contexted(Box::new(err), ctx),
Err(err) => match err.downcast::<ufs::Error>() {
Ok(err) => UError::Contexted(Box::new(UError::FSError(err)), ctx),
Err(err) => UError::Runtime(err.to_string()),
},
}
}
}