replace snafu with custom error impl

This commit is contained in:
glyph 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"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
snafu = "0.6"
tera = { version = "1.12.1", features = ["builtins"] }
xdg = "2.2.0"

View File

@ -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<serde_json::error::Error> 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<serde_yaml::Error> 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<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 {
fn from(err: PeachError) -> PeachWebError {
PeachWebError::PeachLibError {
PeachWebError::PeachLib {
source: err,
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(),
};
Err(PeachWebError::FailedToRegisterDynDomain { msg })
Err(PeachWebError::FailedToRegisterDynDomain(msg))
}
}
}