From 9da8b7588e22a9e84ca015e92c5890ba84e2b2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Sun, 11 Aug 2024 16:15:52 +0200 Subject: [PATCH] dns: derive Error for dns::Error --- crates/dns/Cargo.toml | 1 + crates/dns/src/domain.rs | 2 +- crates/dns/src/lib.rs | 22 ++++++++++++++-------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/dns/Cargo.toml b/crates/dns/Cargo.toml index da1b3947..7ea50edd 100644 --- a/crates/dns/Cargo.toml +++ b/crates/dns/Cargo.toml @@ -11,6 +11,7 @@ license.workspace = true [dependencies] sl-std = { workspace = true } log = { workspace = true } +error-derive = { workspace = true } [lints] workspace = true diff --git a/crates/dns/src/domain.rs b/crates/dns/src/domain.rs index 0407a4c8..26623655 100644 --- a/crates/dns/src/domain.rs +++ b/crates/dns/src/domain.rs @@ -187,7 +187,7 @@ impl Domain { nameserver = DNS_CACHE.get(&ns_domain)?; } else { // We did not make any progress - return Err(DNSError::CouldNotResolve(self.clone())); + return Err(DNSError::CouldNotResolve); } } Err(DNSError::MaxResolutionStepsExceeded) diff --git a/crates/dns/src/lib.rs b/crates/dns/src/lib.rs index 76a615bc..4e4d9344 100644 --- a/crates/dns/src/lib.rs +++ b/crates/dns/src/lib.rs @@ -9,6 +9,7 @@ mod resource_type; use crate::resource_type::{ResourceRecord, ResourceRecordClass}; pub use dns_cache::DNS_CACHE; pub use domain::Domain; +use error_derive::Error; use std::{ io, @@ -23,18 +24,23 @@ const MAX_RESOLUTION_STEPS: usize = 5; /// See [this list of root servers](https://www.iana.org/domains/root/servers). const ROOT_SERVER: IpAddr = IpAddr::V4(Ipv4Addr::new(199, 7, 83, 42)); -#[derive(Debug)] +#[derive(Debug, Error)] pub enum DNSError { + #[msg = "invalid response"] InvalidResponse, - CouldNotResolve(Domain), + + #[msg = "could not resolve"] + CouldNotResolve, + + #[msg = "maximum number of resolution steps exceeded"] MaxResolutionStepsExceeded, + + #[msg = "unexpected id"] UnexpectedID, + + #[msg = "io error"] IO(io::Error), - DomainTooLong, -} -impl From for DNSError { - fn from(value: io::Error) -> Self { - Self::IO(value) - } + #[msg = "domain too long"] + DomainTooLong, }