public_ip/
error.rs

1use std::error::Error as StdError;
2use std::fmt::Debug;
3use std::net::AddrParseError;
4use std::str::Utf8Error;
5
6use thiserror::Error;
7
8#[cfg(feature = "dns-resolver")]
9use crate::dns;
10#[cfg(feature = "http-resolver")]
11use crate::http;
12
13/// An error produced while attempting to resolve.
14#[derive(Debug, Error)]
15#[non_exhaustive]
16pub enum Error {
17    /// No or invalid IP address string found.
18    #[error("no or invalid IP address string found")]
19    Addr,
20    /// IP version not requested was returned.
21    #[error("IP version not requested was returned")]
22    Version,
23    /// DNS resolver error.
24    #[cfg(feature = "dns-resolver")]
25    #[cfg_attr(docsrs, doc(cfg(feature = "dns-resolver")))]
26    #[error("dns resolver: {0}")]
27    Dns(dns::Error),
28    /// HTTP resolver error.
29    #[cfg(feature = "http-resolver")]
30    #[cfg_attr(docsrs, doc(cfg(feature = "http-resolver")))]
31    #[error("http resolver: {0}")]
32    Http(http::Error),
33    /// Other resolver error.
34    #[error("other resolver: {0}")]
35    Other(Box<dyn StdError + Send + Sync + 'static>),
36}
37
38impl Error {
39    /// Construct a new error.
40    pub fn new<E>(error: E) -> Self
41    where
42        E: StdError + Send + Sync + 'static,
43    {
44        Self::Other(Box::new(error))
45    }
46}
47
48#[cfg(feature = "dns-resolver")]
49impl From<dns::Error> for Error {
50    fn from(error: dns::Error) -> Self {
51        Self::Dns(error)
52    }
53}
54
55#[cfg(feature = "http-resolver")]
56impl From<http::Error> for Error {
57    fn from(error: http::Error) -> Self {
58        Self::Http(error)
59    }
60}
61
62impl From<Utf8Error> for Error {
63    fn from(_: Utf8Error) -> Self {
64        Self::Addr
65    }
66}
67
68impl From<AddrParseError> for Error {
69    fn from(_: AddrParseError) -> Self {
70        Self::Addr
71    }
72}