replace snafu with custom error impl

This commit is contained in:
2022-01-12 19:58:49 +02:00
parent bb5cd0f0d3
commit fd94ba27ac
3 changed files with 40 additions and 19 deletions

View File

@ -45,7 +45,6 @@ peach-stats = { path = "../peach-stats", features = ["serde_support"] }
rocket = { version = "0.5.0-rc.1", features = ["json", "secrets"] } rocket = { version = "0.5.0-rc.1", features = ["json", "secrets"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
snafu = "0.6"
tera = { version = "1.12.1", features = ["builtins"] } tera = { version = "1.12.1", features = ["builtins"] }
xdg = "2.2.0" xdg = "2.2.0"

View File

@ -2,35 +2,57 @@
use peach_lib::error::PeachError; use peach_lib::error::PeachError;
use peach_lib::{serde_json, serde_yaml}; use peach_lib::{serde_json, serde_yaml};
use snafu::Snafu; use serde_json::error::Error as JsonError;
use serde_yaml::Error as YamlError;
#[derive(Debug, Snafu)] /// Custom error type encapsulating all possible errors for the web application.
#[derive(Debug)]
pub enum PeachWebError { pub enum PeachWebError {
#[snafu(display("Error loading serde json"))] Json(JsonError),
Serde { source: serde_json::error::Error }, Yaml(YamlError),
#[snafu(display("Error loading peach-config yaml"))] FailedToRegisterDynDomain(String),
YamlError { source: serde_yaml::Error }, PeachLib { source: PeachError, msg: String },
#[snafu(display("{}", msg))]
FailedToRegisterDynDomain { msg: String },
#[snafu(display("{}: {}", source, msg))]
PeachLibError { source: PeachError, msg: String },
} }
impl From<serde_json::error::Error> for PeachWebError { impl std::error::Error for PeachWebError {
fn from(err: serde_json::error::Error) -> PeachWebError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
PeachWebError::Serde { source: err } match *self {
PeachWebError::Json(ref source) => Some(source),
PeachWebError::Yaml(ref source) => Some(source),
PeachWebError::FailedToRegisterDynDomain(_) => None,
PeachWebError::PeachLib { ref source, .. } => Some(source),
}
} }
} }
impl From<serde_yaml::Error> for PeachWebError { impl std::fmt::Display for PeachWebError {
fn from(err: serde_yaml::Error) -> PeachWebError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
PeachWebError::YamlError { source: err } match *self {
PeachWebError::Json(ref source) => write!(f, "Serde JSON error: {}", source),
PeachWebError::Yaml(ref source) => write!(f, "Serde YAML error: {}", source),
PeachWebError::FailedToRegisterDynDomain(ref msg) => {
write!(f, "DYN DNS error: {}", msg)
}
PeachWebError::PeachLib { ref source, .. } => write!(f, "{}", source),
}
}
}
impl From<JsonError> for PeachWebError {
fn from(err: JsonError) -> PeachWebError {
PeachWebError::Json(err)
}
}
impl From<YamlError> for PeachWebError {
fn from(err: YamlError) -> PeachWebError {
PeachWebError::Yaml(err)
} }
} }
impl From<PeachError> for PeachWebError { impl From<PeachError> for PeachWebError {
fn from(err: PeachError) -> PeachWebError { fn from(err: PeachError) -> PeachWebError {
PeachWebError::PeachLibError { PeachWebError::PeachLib {
source: err, source: err,
msg: "".to_string(), msg: "".to_string(),
} }

View File

@ -65,7 +65,7 @@ pub fn save_dns_configuration(dns_form: DnsForm) -> Result<(), PeachWebError> {
} }
_ => "Failed to register dyndns domain".to_string(), _ => "Failed to register dyndns domain".to_string(),
}; };
Err(PeachWebError::FailedToRegisterDynDomain { msg }) Err(PeachWebError::FailedToRegisterDynDomain(msg))
} }
} }
} }