nitox/
error.rs

1use super::protocol;
2use std::io;
3
4macro_rules! from_error {
5    ($type:ty, $target:ident, $targetvar:expr) => {
6        impl From<$type> for $target {
7            fn from(s: $type) -> Self {
8                $targetvar(s.into())
9            }
10        }
11    };
12}
13
14/// Error enum for all cases of internal/external errors occuring during client execution
15#[derive(Debug, Fail)]
16pub enum NatsError {
17    /// Building a command has failed because of invalid syntax or incorrect arguments
18    #[fail(display = "CommandBuildError: {}", _0)]
19    CommandBuildError(String),
20    /// Generic IO error from stdlib
21    #[fail(display = "IOError: {:?}", _0)]
22    IOError(io::Error),
23    /// Occurs when the client is not yet connected or got disconnected from the server.
24    /// Contains `Some<io::Error>` when it's actually a disconnection or contains `None` when we are not connected at all
25    #[fail(display = "ServerDisconnected: {:?}", _0)]
26    ServerDisconnected(Option<io::Error>),
27    /// Protocol error
28    #[fail(display = "ProtocolError: {}", _0)]
29    ProtocolError(protocol::CommandError),
30    /// Occurs if we try to parse a string that is supposed to be valid UTF8 and...is actually not
31    #[fail(display = "UTF8Error: {}", _0)]
32    UTF8Error(::std::string::FromUtf8Error),
33    /// Error on TLS handling
34    #[fail(display = "TlsError: {}", _0)]
35    TlsError(::native_tls::Error),
36    /// Occurs when the host is not provided, removing the ability for TLS to function correctly for server identify verification
37    #[fail(display = "TlsHostMissingError: Host is missing, can't verify server identity")]
38    TlsHostMissingError,
39    /// Cannot parse an URL
40    #[fail(display = "UrlParseError: {}", _0)]
41    UrlParseError(::url::ParseError),
42    /// Cannot parse an IP
43    #[fail(display = "AddrParseError: {}", _0)]
44    AddrParseError(::std::net::AddrParseError),
45    /// Occurs when we cannot resolve the URI given using the local host's DNS resolving mechanisms
46    /// Will contain `Some(io::Error)` when the resolving has been tried with an error, and `None` when
47    /// resolving succeeded but gave no results
48    #[fail(display = "UriDNSResolveError: {:?}", _0)]
49    UriDNSResolveError(Option<io::Error>),
50    /// Cannot reconnect to server after retrying once
51    #[fail(display = "CannotReconnectToServer: cannot reconnect to server")]
52    CannotReconnectToServer,
53    /// Something went wrong in one of the Reciever/Sender pairs
54    #[fail(display = "InnerBrokenChain: the sender/receiver pair has been disconnected")]
55    InnerBrokenChain,
56    /// The user supplied a too big payload for the server
57    #[fail(
58        display = "MaxPayloadOverflow: the given payload exceeds the server setting (max_payload_size = {})",
59        _0
60    )]
61    MaxPayloadOverflow(u32),
62    /// Generic string error
63    #[fail(display = "GenericError: {}", _0)]
64    GenericError(String),
65    /// Error thrown when a subscription is fused after reaching the maximum messages
66    #[fail(display = "SubscriptionReachedMaxMsgs after {} messages", _0)]
67    SubscriptionReachedMaxMsgs(u32),
68}
69
70impl From<io::Error> for NatsError {
71    fn from(err: io::Error) -> Self {
72        match err.kind() {
73            io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionRefused => {
74                NatsError::ServerDisconnected(Some(err))
75            }
76            _ => NatsError::IOError(err),
77        }
78    }
79}
80
81impl<T> From<::futures::sync::mpsc::SendError<T>> for NatsError {
82    fn from(_: ::futures::sync::mpsc::SendError<T>) -> Self {
83        NatsError::InnerBrokenChain
84    }
85}
86
87from_error!(protocol::CommandError, NatsError, NatsError::ProtocolError);
88from_error!(::std::string::FromUtf8Error, NatsError, NatsError::UTF8Error);
89from_error!(::native_tls::Error, NatsError, NatsError::TlsError);
90from_error!(String, NatsError, NatsError::GenericError);
91from_error!(::url::ParseError, NatsError, NatsError::UrlParseError);
92from_error!(::std::net::AddrParseError, NatsError, NatsError::AddrParseError);