implement error trait on domain config store errors

This commit is contained in:
Brian Picciano 2023-05-09 13:52:21 +02:00
parent 97071c1b87
commit a8c96f9a2e
2 changed files with 16 additions and 20 deletions

View File

@ -96,6 +96,7 @@ impl Checker for DNSChecker {
.map_err(|e| CheckDomainError::Unexpected(Box::from(e)))? .map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?
.append_domain(&fqdn) .append_domain(&fqdn)
.map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?; .map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?;
let response = self let response = self
.client .client
.query(&fqdn, DNSClass::IN, RecordType::TXT) .query(&fqdn, DNSClass::IN, RecordType::TXT)

View File

@ -13,30 +13,21 @@ pub struct Config {
pub origin_descr: Descr, pub origin_descr: Descr,
} }
#[derive(Debug)] #[derive(thiserror::Error, Debug)]
pub enum GetError { pub enum GetError {
#[error("not found")]
NotFound, NotFound,
#[error(transparent)]
Unexpected(Box<dyn Error>), Unexpected(Box<dyn Error>),
} }
impl<E: Error + 'static> From<E> for GetError { #[derive(thiserror::Error, Debug)]
fn from(e: E) -> GetError {
GetError::Unexpected(Box::from(e))
}
}
#[derive(Debug)]
pub enum SetError { pub enum SetError {
NotFound, #[error(transparent)]
Unexpected(Box<dyn Error>), Unexpected(Box<dyn Error>),
} }
impl<E: Error + 'static> From<E> for SetError {
fn from(e: E) -> SetError {
SetError::Unexpected(Box::from(e))
}
}
#[automock] #[automock]
pub trait Store { pub trait Store {
fn get(&self, domain: &str) -> Result<Config, GetError>; fn get(&self, domain: &str) -> Result<Config, GetError>;
@ -69,17 +60,21 @@ impl Store for FSStore {
let config_file = let config_file =
fs::File::open(self.config_file_path(domain)).map_err(|e| match e.kind() { fs::File::open(self.config_file_path(domain)).map_err(|e| match e.kind() {
io::ErrorKind::NotFound => GetError::NotFound, 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> { 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))?; let config_file = fs::File::create(self.config_file_path(domain))
serde_json::to_writer(config_file, config)?; .map_err(|e| SetError::Unexpected(Box::from(e)))?;
serde_json::to_writer(config_file, config)
.map_err(|e| SetError::Unexpected(Box::from(e)))?;
Ok(()) Ok(())
} }