peach-workspace/peach-web/src/error.rs

94 lines
2.9 KiB
Rust
Raw Normal View History

2021-11-03 09:52:02 +00:00
//! Custom error type representing all possible error variants for peach-web.
2021-08-06 17:58:40 +00:00
use std::io::Error as IoError;
2022-02-11 08:31:31 +00:00
use golgi::GolgiError;
2021-08-06 17:58:40 +00:00
use peach_lib::error::PeachError;
use peach_lib::{serde_json, serde_yaml};
2022-01-12 17:58:49 +00:00
use serde_json::error::Error as JsonError;
use serde_yaml::Error as YamlError;
2021-08-06 17:58:40 +00:00
2022-01-12 17:58:49 +00:00
/// Custom error type encapsulating all possible errors for the web application.
#[derive(Debug)]
2021-08-06 17:58:40 +00:00
pub enum PeachWebError {
FailedToRegisterDynDomain(String),
2022-02-11 08:31:31 +00:00
Golgi(GolgiError),
HomeDir,
Io(IoError),
2022-01-12 17:58:49 +00:00
Json(JsonError),
OsString,
2022-01-12 17:58:49 +00:00
PeachLib { source: PeachError, msg: String },
Yaml(YamlError),
2021-08-06 17:58:40 +00:00
}
2022-01-12 17:58:49 +00:00
impl std::error::Error for PeachWebError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
PeachWebError::FailedToRegisterDynDomain(_) => None,
2022-02-11 08:31:31 +00:00
PeachWebError::Golgi(ref source) => Some(source),
PeachWebError::HomeDir => None,
PeachWebError::Io(ref source) => Some(source),
2022-01-12 17:58:49 +00:00
PeachWebError::Json(ref source) => Some(source),
PeachWebError::OsString => None,
2022-01-12 17:58:49 +00:00
PeachWebError::PeachLib { ref source, .. } => Some(source),
PeachWebError::Yaml(ref source) => Some(source),
2022-01-12 17:58:49 +00:00
}
}
}
impl std::fmt::Display for PeachWebError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
PeachWebError::FailedToRegisterDynDomain(ref msg) => {
write!(f, "DYN DNS error: {}", msg)
}
PeachWebError::Golgi(ref source) => write!(f, "Golgi error: {}", source),
PeachWebError::HomeDir => write!(
f,
"Filesystem error: failed to determine home directory path"
),
PeachWebError::Io(ref source) => write!(f, "IO error: {}", source),
PeachWebError::Json(ref source) => write!(f, "Serde JSON error: {}", source),
PeachWebError::OsString => write!(
f,
"Filesystem error: failed to convert OsString to String for go-ssb directory path"
),
2022-01-12 17:58:49 +00:00
PeachWebError::PeachLib { ref source, .. } => write!(f, "{}", source),
PeachWebError::Yaml(ref source) => write!(f, "Serde YAML error: {}", source),
2022-01-12 17:58:49 +00:00
}
}
}
2022-02-11 08:31:31 +00:00
impl From<GolgiError> for PeachWebError {
fn from(err: GolgiError) -> PeachWebError {
PeachWebError::Golgi(err)
}
}
impl From<IoError> for PeachWebError {
fn from(err: IoError) -> PeachWebError {
PeachWebError::Io(err)
}
}
2022-01-12 17:58:49 +00:00
impl From<JsonError> for PeachWebError {
fn from(err: JsonError) -> PeachWebError {
PeachWebError::Json(err)
2021-08-06 17:58:40 +00:00
}
}
impl From<PeachError> for PeachWebError {
fn from(err: PeachError) -> PeachWebError {
2022-01-12 17:58:49 +00:00
PeachWebError::PeachLib {
2021-08-06 17:58:40 +00:00
source: err,
msg: "".to_string(),
}
}
}
impl From<YamlError> for PeachWebError {
fn from(err: YamlError) -> PeachWebError {
PeachWebError::Yaml(err)
}
}