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)))?
.append_domain(&fqdn)
.map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?;
let response = self
.client
.query(&fqdn, DNSClass::IN, RecordType::TXT)

View File

@ -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<dyn Error>),
}
impl<E: Error + 'static> From<E> 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<dyn Error>),
}
impl<E: Error + 'static> From<E> for SetError {
fn from(e: E) -> SetError {
SetError::Unexpected(Box::from(e))
}
}
#[automock]
pub trait Store {
fn get(&self, domain: &str) -> Result<Config, GetError>;
@ -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(())
}