Skip to main content

ntex_net/connect/
error.rs

1use std::io;
2
3use ntex_error::ErrorDiagnostic;
4
5#[derive(thiserror::Error, Debug)]
6pub enum ConnectError {
7    /// Failed to resolve the hostname
8    #[error("Failed resolving hostname: {0}")]
9    Resolver(io::Error),
10
11    /// No dns records
12    #[error("No dns records found for the input")]
13    NoRecords,
14
15    /// Invalid input
16    #[error("Invalid input")]
17    InvalidInput,
18
19    /// Unresolved host name
20    #[error("Connector received `Connect` method with unresolved host")]
21    Unresolved,
22
23    /// Connection io error
24    #[error("Tcp connect")]
25    Io(#[from] io::Error),
26}
27
28impl Clone for ConnectError {
29    fn clone(&self) -> Self {
30        match self {
31            ConnectError::Resolver(err) => {
32                ConnectError::Resolver(io::Error::new(err.kind(), format!("{err}")))
33            }
34            ConnectError::NoRecords => ConnectError::NoRecords,
35            ConnectError::InvalidInput => ConnectError::InvalidInput,
36            ConnectError::Unresolved => ConnectError::Unresolved,
37            ConnectError::Io(err) => ConnectError::Io(io::Error::new(err.kind(), format!("{err}"))),
38        }
39    }
40}
41
42impl ErrorDiagnostic for ConnectError {
43    fn signature(&self) -> &'static str {
44        match self {
45            ConnectError::InvalidInput => "ntex-connect-InvalidInput",
46            ConnectError::Resolver(_) => "ntex-connect-Resolver",
47            ConnectError::NoRecords => "ntex-connect-NoRecords",
48            ConnectError::Unresolved => "ntex-connect-Unresolved",
49            ConnectError::Io(err) => err.signature(),
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    /// STATEMENT: Cloning a `ConnectError` keeps its variant, and for `Resolver` and `Io`
59    /// rebuilds the `io::Error` with the same kind and message.
60    ///
61    /// HOW:
62    /// - Clone each unit variant and check the variant with `matches!`.
63    /// - Clone `Resolver` and `Io` errors with distinct kinds; check the clone's variant,
64    ///   `kind()` and message.
65    #[test]
66    #[allow(clippy::redundant_clone)]
67    fn connect_error_clone() {
68        assert!(matches!(
69            ConnectError::NoRecords.clone(),
70            ConnectError::NoRecords
71        ));
72        assert!(matches!(
73            ConnectError::InvalidInput.clone(),
74            ConnectError::InvalidInput
75        ));
76        assert!(matches!(
77            ConnectError::Unresolved.clone(),
78            ConnectError::Unresolved
79        ));
80
81        let err = ConnectError::Resolver(io::Error::new(io::ErrorKind::NotFound, "no host"));
82        let ConnectError::Resolver(e) = err.clone() else {
83            panic!("expected Resolver")
84        };
85        assert_eq!(e.kind(), io::ErrorKind::NotFound);
86        assert_eq!(e.to_string(), "no host");
87
88        let err = ConnectError::Io(io::Error::new(io::ErrorKind::ConnectionRefused, "refused"));
89        let ConnectError::Io(e) = err.clone() else {
90            panic!("expected Io")
91        };
92        assert_eq!(e.kind(), io::ErrorKind::ConnectionRefused);
93        assert_eq!(e.to_string(), "refused");
94    }
95
96    /// STATEMENT: `ConnectError` signatures name the connect error variant, or the I/O error
97    /// kind for I/O errors.
98    ///
99    /// HOW:
100    /// - Expect `ntex-connect-*` signatures for the input, resolver and record errors.
101    /// - Expect `std-io-*` signatures with the kind for I/O errors.
102    #[test]
103    fn error_diagnostic() {
104        let err = ConnectError::InvalidInput;
105        assert_eq!(err.signature(), "ntex-connect-InvalidInput");
106
107        let err = ConnectError::Resolver(io::Error::other("test"));
108        assert_eq!(err.signature(), "ntex-connect-Resolver");
109
110        let err = ConnectError::NoRecords;
111        assert_eq!(err.signature(), "ntex-connect-NoRecords");
112
113        let err = ConnectError::Unresolved;
114        assert_eq!(err.signature(), "ntex-connect-Unresolved");
115
116        let err = ConnectError::Io(io::Error::new(io::ErrorKind::InvalidInput, "test"));
117        assert_eq!(err.signature(), "std-io-InvalidInput");
118
119        let err = ConnectError::Io(io::Error::other("test"));
120        assert_eq!(err.signature(), "std-io-Error");
121    }
122}