hickory_resolver/
error.rsuse std::{fmt, io, sync};
use thiserror::Error;
use crate::proto::{
rr::{rdata::SOA, Record},
xfer::retry_dns_handle::RetryableError,
ProtoError, ProtoErrorKind,
};
#[cfg(feature = "backtrace")]
use crate::proto::{trace, ExtBacktrace};
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ResolveErrorKind {
#[error("{0}")]
Message(&'static str),
#[error("{0}")]
Msg(String),
#[error("proto error: {0}")]
Proto(#[from] ProtoError),
}
impl Clone for ResolveErrorKind {
fn clone(&self) -> Self {
use self::ResolveErrorKind::*;
match self {
Message(msg) => Message(msg),
Msg(msg) => Msg(msg.clone()),
Proto(proto) => Self::from(proto.clone()),
}
}
}
#[derive(Debug, Clone, Error)]
pub struct ResolveError {
pub(crate) kind: ResolveErrorKind,
#[cfg(feature = "backtrace")]
backtrack: Option<ExtBacktrace>,
}
impl ResolveError {
pub fn kind(&self) -> &ResolveErrorKind {
&self.kind
}
pub fn into_kind(self) -> ResolveErrorKind {
self.kind
}
pub fn proto(&self) -> Option<&ProtoError> {
match &self.kind {
ResolveErrorKind::Proto(proto) => Some(proto),
_ => None,
}
}
pub fn is_nx_domain(&self) -> bool {
self.proto()
.map(|proto| proto.is_nx_domain())
.unwrap_or(false)
}
pub fn is_no_records_found(&self) -> bool {
self.proto()
.map(|proto| proto.is_no_records_found())
.unwrap_or(false)
}
pub fn into_soa(self) -> Option<Box<Record<SOA>>> {
match self.kind {
ResolveErrorKind::Proto(proto) => proto.into_soa(),
_ => None,
}
}
}
impl RetryableError for ResolveError {
fn should_retry(&self) -> bool {
match self.kind() {
ResolveErrorKind::Message(_) | ResolveErrorKind::Msg(_) => false,
ResolveErrorKind::Proto(proto) => proto.should_retry(),
}
}
fn attempted(&self) -> bool {
match self.kind() {
ResolveErrorKind::Proto(e) => e.attempted(),
_ => true,
}
}
}
impl fmt::Display for ResolveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cfg_if::cfg_if! {
if #[cfg(feature = "backtrace")] {
if let Some(backtrace) = &self.backtrack {
fmt::Display::fmt(&self.kind, f)?;
fmt::Debug::fmt(backtrace, f)
} else {
fmt::Display::fmt(&self.kind, f)
}
} else {
fmt::Display::fmt(&self.kind, f)
}
}
}
}
impl From<ResolveErrorKind> for ResolveError {
fn from(kind: ResolveErrorKind) -> Self {
Self {
kind,
#[cfg(feature = "backtrace")]
backtrack: trace!(),
}
}
}
impl From<&'static str> for ResolveError {
fn from(msg: &'static str) -> Self {
ResolveErrorKind::Message(msg).into()
}
}
impl TryFrom<ResolveError> for ProtoErrorKind {
type Error = ResolveError;
fn try_from(error: ResolveError) -> Result<Self, Self::Error> {
match error.kind {
ResolveErrorKind::Proto(p) => Ok(*p.kind),
_ => Err(error),
}
}
}
#[cfg(target_os = "windows")]
#[cfg(feature = "system-config")]
impl From<ipconfig::error::Error> for ResolveError {
fn from(e: ipconfig::error::Error) -> ResolveError {
ResolveErrorKind::Msg(format!("failed to read from registry: {}", e)).into()
}
}
impl From<String> for ResolveError {
fn from(msg: String) -> Self {
ResolveErrorKind::Msg(msg).into()
}
}
impl From<io::Error> for ResolveError {
fn from(e: io::Error) -> Self {
ResolveErrorKind::from(ProtoError::from(e)).into()
}
}
impl From<ProtoError> for ResolveError {
fn from(e: ProtoError) -> Self {
ResolveErrorKind::Proto(e).into()
}
}
impl From<ResolveError> for io::Error {
fn from(e: ResolveError) -> Self {
Self::new(io::ErrorKind::Other, e)
}
}
impl<T> From<sync::PoisonError<T>> for ResolveError {
fn from(e: sync::PoisonError<T>) -> Self {
ResolveErrorKind::Msg(format!("lock was poisoned, this is non-recoverable: {e}")).into()
}
}