diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index f0b6d69..fbaced9 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -45,7 +45,6 @@ peach-stats = { path = "../peach-stats", features = ["serde_support"] } rocket = { version = "0.5.0-rc.1", features = ["json", "secrets"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -snafu = "0.6" tera = { version = "1.12.1", features = ["builtins"] } xdg = "2.2.0" diff --git a/peach-web/src/error.rs b/peach-web/src/error.rs index a24d253..6288b6b 100644 --- a/peach-web/src/error.rs +++ b/peach-web/src/error.rs @@ -2,35 +2,57 @@ use peach_lib::error::PeachError; 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 { - #[snafu(display("Error loading serde json"))] - Serde { source: serde_json::error::Error }, - #[snafu(display("Error loading peach-config yaml"))] - YamlError { source: serde_yaml::Error }, - #[snafu(display("{}", msg))] - FailedToRegisterDynDomain { msg: String }, - #[snafu(display("{}: {}", source, msg))] - PeachLibError { source: PeachError, msg: String }, + Json(JsonError), + Yaml(YamlError), + FailedToRegisterDynDomain(String), + PeachLib { source: PeachError, msg: String }, } -impl From for PeachWebError { - fn from(err: serde_json::error::Error) -> PeachWebError { - PeachWebError::Serde { source: err } +impl std::error::Error for PeachWebError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + 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 for PeachWebError { - fn from(err: serde_yaml::Error) -> PeachWebError { - PeachWebError::YamlError { source: err } +impl std::fmt::Display for PeachWebError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + 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 for PeachWebError { + fn from(err: JsonError) -> PeachWebError { + PeachWebError::Json(err) + } +} + +impl From for PeachWebError { + fn from(err: YamlError) -> PeachWebError { + PeachWebError::Yaml(err) } } impl From for PeachWebError { fn from(err: PeachError) -> PeachWebError { - PeachWebError::PeachLibError { + PeachWebError::PeachLib { source: err, msg: "".to_string(), } diff --git a/peach-web/src/routes/settings/dns.rs b/peach-web/src/routes/settings/dns.rs index d9bf10a..7abfd11 100644 --- a/peach-web/src/routes/settings/dns.rs +++ b/peach-web/src/routes/settings/dns.rs @@ -65,7 +65,7 @@ pub fn save_dns_configuration(dns_form: DnsForm) -> Result<(), PeachWebError> { } _ => "Failed to register dyndns domain".to_string(), }; - Err(PeachWebError::FailedToRegisterDynDomain { msg }) + Err(PeachWebError::FailedToRegisterDynDomain(msg)) } } }