From a8c96f9a2eda04e9e640210f2f612a23c1226cc4 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 9 May 2023 13:52:21 +0200 Subject: [PATCH] implement error trait on domain config store errors --- src/domain/checker.rs | 1 + src/domain/config.rs | 35 +++++++++++++++-------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/domain/checker.rs b/src/domain/checker.rs index 45983a4..b2592aa 100644 --- a/src/domain/checker.rs +++ b/src/domain/checker.rs @@ -96,6 +96,7 @@ impl Checker for DNSChecker { .map_err(|e| CheckDomainError::Unexpected(Box::from(e)))? .append_domain(&fqdn) .map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?; + let response = self .client .query(&fqdn, DNSClass::IN, RecordType::TXT) diff --git a/src/domain/config.rs b/src/domain/config.rs index d954495..b66b288 100644 --- a/src/domain/config.rs +++ b/src/domain/config.rs @@ -13,30 +13,21 @@ pub struct Config { pub origin_descr: Descr, } -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub enum GetError { + #[error("not found")] NotFound, + + #[error(transparent)] Unexpected(Box), } -impl From for GetError { - fn from(e: E) -> GetError { - GetError::Unexpected(Box::from(e)) - } -} - -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub enum SetError { - NotFound, + #[error(transparent)] Unexpected(Box), } -impl From for SetError { - fn from(e: E) -> SetError { - SetError::Unexpected(Box::from(e)) - } -} - #[automock] pub trait Store { fn get(&self, domain: &str) -> Result; @@ -69,17 +60,21 @@ impl Store for FSStore { let config_file = fs::File::open(self.config_file_path(domain)).map_err(|e| match e.kind() { io::ErrorKind::NotFound => GetError::NotFound, - _ => e.into(), + _ => GetError::Unexpected(Box::from(e)), })?; - Ok(serde_json::from_reader(config_file)?) + Ok(serde_json::from_reader(config_file).map_err(|e| GetError::Unexpected(Box::from(e)))?) } fn set(&self, domain: &str, config: &Config) -> Result<(), SetError> { - fs::create_dir_all(self.config_dir_path(domain))?; + fs::create_dir_all(self.config_dir_path(domain)) + .map_err(|e| SetError::Unexpected(Box::from(e)))?; - let config_file = fs::File::create(self.config_file_path(domain))?; - serde_json::to_writer(config_file, config)?; + let config_file = fs::File::create(self.config_file_path(domain)) + .map_err(|e| SetError::Unexpected(Box::from(e)))?; + + serde_json::to_writer(config_file, config) + .map_err(|e| SetError::Unexpected(Box::from(e)))?; Ok(()) }