rants/types/
error.rs

1//! `nats` `Error` and `Result`
2
3use std::{fmt, io};
4
5#[cfg(feature = "tls")]
6use crate::types::tls::TlsError;
7use crate::{types::Sid, util};
8
9use tokio::time;
10
11/// All potential `rants` errors
12#[derive(Debug)]
13pub enum Error {
14    /// Occurs when trying to a publish greater than the `max_payload` of the server
15    ExceedsMaxPayload {
16        /// The number of bytes tried
17        tried: usize,
18        /// The `max_payload` set by the server
19        limit: usize,
20    },
21    /// Occurs when trying to parse an invalid [`Address`](../struct.Address.html)
22    InvalidAddress(String),
23    /// Occurs when input is not a valid DNS name
24    #[cfg(feature = "rustls-tls")]
25    InvalidDnsName(String),
26    /// Occurs when trying to parse an [`Address`](../struct.Address.html) with an invalid network
27    /// scheme
28    InvalidNetworkScheme(String),
29    /// Occurs when trying to parse an invalid control line from the server
30    InvalidServerControl(String),
31    /// Occurs when trying to parse an invalid [`Subject`](../struct.Subject.html)
32    InvalidSubject(String),
33    /// Occurs when the payload of a server message has an invalid terminator
34    InvalidTerminator(Vec<u8>),
35    /// Wrapper for all IO errors
36    Io(io::Error),
37    /// Wrapper for all tls errors
38    #[cfg(feature = "tls")]
39    Tls(TlsError),
40    /// Occurs when a [`request`](../struct.Client.html#method.request) does not receive a
41    /// response
42    NoResponse,
43    /// Occurs when trying to use the [`Client`](../struct.Client.html) to communicate with the
44    /// server while not in the [`Connected`](../enum.ClientState.html#variant.Connected) state
45    NotConnected,
46    /// Occurs when the server did not send enough data
47    NotEnoughData,
48    /// A timeout that has elapsed. For example: when a request does not a receive a response
49    /// before the provided timeout duration has expired.
50    Timeout(time::error::Elapsed),
51    /// Occurs when no TLS connector was specified, but the server requires a TLS connection.
52    TlsDisabled,
53    /// Occurs when trying to [`unsubscribe`](../struct.Client.html#method.unsubscribe) with
54    /// an unknown [`Sid`](../type.Sid.html)
55    UnknownSid(Sid),
56}
57
58impl Error {
59    /// Returns true if the error is a `NotConnected` error
60    pub fn not_connected(&self) -> bool {
61        matches!(self, Self::NotConnected)
62    }
63}
64
65impl fmt::Display for Error {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            Error::ExceedsMaxPayload { tried, limit } => {
69                write!(f, "'{}' exceeds max payload '{}'", tried, limit)
70            }
71            Error::InvalidAddress(address) => write!(f, "invalid address {:?}", address),
72            #[cfg(feature = "rustls-tls")]
73            Error::InvalidDnsName(input) => write!(f, "invalid DNS name '{}'", input),
74            Error::InvalidServerControl(line) => write!(f, "invalid control line {:?}", line),
75            Error::InvalidSubject(subject) => write!(f, "invalid subject {:?}", subject),
76            Error::InvalidTerminator(terminator) => {
77                write!(f, "invalid message terminator {:?}", terminator)
78            }
79            Error::Io(e) => write!(f, "{}", e),
80            #[cfg(feature = "tls")]
81            Error::Tls(e) => write!(f, "{}", e),
82            Error::NoResponse => write!(f, "no response"),
83            Error::NotConnected => write!(f, "not connected"),
84            Error::NotEnoughData => write!(f, "not enough data"),
85            Error::InvalidNetworkScheme(protocol) => write!(
86                f,
87                "invalid scheme '{}' only '{}' is supported",
88                protocol,
89                util::NATS_NETWORK_SCHEME
90            ),
91            Error::UnknownSid(sid) => write!(f, "unknown sid '{}'", sid),
92            Error::Timeout(e) => write!(f, "{}", e),
93            Error::TlsDisabled => write!(f, "no TLS connector specified"),
94        }
95    }
96}
97
98impl std::error::Error for Error {}
99
100impl From<tokio::time::error::Elapsed> for Error {
101    fn from(e: tokio::time::error::Elapsed) -> Self {
102        Error::Timeout(e)
103    }
104}
105
106impl From<io::Error> for Error {
107    fn from(e: io::Error) -> Self {
108        Error::Io(e)
109    }
110}
111
112#[cfg(feature = "tls")]
113impl From<TlsError> for Error {
114    fn from(e: TlsError) -> Self {
115        Error::Tls(e)
116    }
117}
118
119/// A `Result` that uses the `rants` [`Error`](enum.Error.html) type
120pub type Result<T> = std::result::Result<T, Error>;