use std::{fmt, io, sync};
use thiserror::Error;
use crate::proto::{error::ProtoError, xfer::retry_dns_handle::RetryableError};
#[cfg(feature = "backtrace")]
use crate::proto::{trace, ExtBacktrace};
pub type ResolveResult<T> = ::std::result::Result<T, ResolveError>;
#[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(ref 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 proto(&self) -> Option<&ProtoError> {
match self.kind {
ResolveErrorKind::Proto(ref proto) => Some(proto),
_ => 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(ref 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()
}
}
#[cfg(target_os = "windows")]
#[cfg(feature = "system-config")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "system-config", windows))))]
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()
}
}