1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::error::Error as StdError;
use std::fmt::Debug;
use std::net::AddrParseError;
use std::str::Utf8Error;

use thiserror::Error;

#[cfg(feature = "dns-resolver")]
use crate::dns;
#[cfg(feature = "http-resolver")]
use crate::http;

/// An error produced while attempting to resolve.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
    /// No or invalid IP address string found.
    #[error("no or invalid IP address string found")]
    Addr,
    /// IP version not requested was returned.
    #[error("IP version not requested was returned")]
    Version,
    /// DNS resolver error.
    #[cfg(feature = "dns-resolver")]
    #[cfg_attr(docsrs, doc(cfg(feature = "dns-resolver")))]
    #[error("dns resolver: {0}")]
    Dns(dns::Error),
    /// HTTP resolver error.
    #[cfg(feature = "http-resolver")]
    #[cfg_attr(docsrs, doc(cfg(feature = "http-resolver")))]
    #[error("http resolver: {0}")]
    Http(http::Error),
    /// Other resolver error.
    #[error("other resolver: {0}")]
    Other(Box<dyn StdError + Send + Sync + 'static>),
}

impl Error {
    /// Construct a new error.
    pub fn new<E>(error: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::Other(Box::new(error))
    }
}

#[cfg(feature = "dns-resolver")]
impl From<dns::Error> for Error {
    fn from(error: dns::Error) -> Self {
        Self::Dns(error)
    }
}

#[cfg(feature = "http-resolver")]
impl From<http::Error> for Error {
    fn from(error: http::Error) -> Self {
        Self::Http(error)
    }
}

impl From<Utf8Error> for Error {
    fn from(_: Utf8Error) -> Self {
        Self::Addr
    }
}

impl From<AddrParseError> for Error {
    fn from(_: AddrParseError) -> Self {
        Self::Addr
    }
}