make dns checker errors implement error trait

This commit is contained in:
Brian Picciano 2023-05-09 13:44:57 +02:00
parent be84742e94
commit 97071c1b87
2 changed files with 31 additions and 21 deletions

View File

@ -6,31 +6,31 @@ use trust_dns_client::client::{Client, SyncClient};
use trust_dns_client::rr::{DNSClass, Name, RData, RecordType};
use trust_dns_client::udp::UdpClientConnection;
#[derive(Debug)]
#[derive(thiserror::Error, Debug)]
pub enum NewDNSCheckerError {
#[error("invalid resolver address")]
InvalidResolverAddress,
#[error("invalid target CNAME")]
InvalidTargetCNAME,
#[error(transparent)]
Unexpected(Box<dyn Error>),
}
impl<E: Error + 'static> From<E> for NewDNSCheckerError {
fn from(e: E) -> NewDNSCheckerError {
NewDNSCheckerError::Unexpected(Box::from(e))
}
}
#[derive(Debug)]
#[derive(thiserror::Error, Debug)]
pub enum CheckDomainError {
#[error("invalid domain name")]
InvalidDomainName,
TargetCNAMENotSet,
ChallengeTokenNotSet,
Unexpected(Box<dyn Error>),
}
impl<E: Error + 'static> From<E> for CheckDomainError {
fn from(e: E) -> CheckDomainError {
CheckDomainError::Unexpected(Box::from(e))
}
#[error("target CNAME not set")]
TargetCNAMENotSet,
#[error("challenge token not set")]
ChallengeTokenNotSet,
#[error(transparent)]
Unexpected(Box<dyn Error>),
}
#[automock]
@ -52,7 +52,9 @@ impl DNSChecker {
.parse()
.map_err(|_| NewDNSCheckerError::InvalidResolverAddress)?;
let conn = UdpClientConnection::new(resolver_addr)?;
let conn = UdpClientConnection::new(resolver_addr)
.map_err(|e| NewDNSCheckerError::Unexpected(Box::from(e)))?;
let client = SyncClient::new(conn);
Ok(DNSChecker {
@ -69,7 +71,10 @@ impl Checker for DNSChecker {
// check that the CNAME is installed correctly on the domain
{
let response = self.client.query(&fqdn, DNSClass::IN, RecordType::CNAME)?;
let response = self
.client
.query(&fqdn, DNSClass::IN, RecordType::CNAME)
.map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?;
let records = response.answers();
@ -87,8 +92,14 @@ impl Checker for DNSChecker {
// check that the TXT record with the challenge token is correctly installed on the domain
{
let fqdn = Name::from_str("_gateway")?.append_domain(&fqdn)?;
let response = self.client.query(&fqdn, DNSClass::IN, RecordType::TXT)?;
let fqdn = Name::from_str("_gateway")
.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)
.map_err(|e| CheckDomainError::Unexpected(Box::from(e)))?;
let records = response.answers();

View File

@ -1,4 +1,3 @@
mod descr;
pub use self::descr::Descr;
pub mod store;