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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! `nats` `Error` and `Result`
use std::{fmt, io};
#[cfg(feature = "tls")]
use crate::types::tls::TlsError;
use crate::{types::Sid, util};
use tokio::time;
/// All potential `rants` errors
#[derive(Debug)]
pub enum Error {
/// Occurs when trying to a publish greater than the `max_payload` of the server
ExceedsMaxPayload {
/// The number of bytes tried
tried: usize,
/// The `max_payload` set by the server
limit: usize,
},
/// Occurs when trying to parse an invalid [`Address`](../struct.Address.html)
InvalidAddress(String),
/// Occurs when input is not a valid DNS name
#[cfg(feature = "rustls-tls")]
InvalidDnsName(String),
/// Occurs when trying to parse an [`Address`](../struct.Address.html) with an invalid network
/// scheme
InvalidNetworkScheme(String),
/// Occurs when trying to parse an invalid control line from the server
InvalidServerControl(String),
/// Occurs when trying to parse an invalid [`Subject`](../struct.Subject.html)
InvalidSubject(String),
/// Occurs when the payload of a server message has an invalid terminator
InvalidTerminator(Vec<u8>),
/// Wrapper for all IO errors
Io(io::Error),
/// Wrapper for all tls errors
#[cfg(feature = "tls")]
Tls(TlsError),
/// Occurs when a [`request`](../struct.Client.html#method.request) does not receive a
/// response
NoResponse,
/// Occurs when trying to use the [`Client`](../struct.Client.html) to communicate with the
/// server while not in the [`Connected`](../enum.ClientState.html#variant.Connected) state
NotConnected,
/// Occurs when the server did not send enough data
NotEnoughData,
/// A timeout that has elapsed. For example: when a request does not a receive a response
/// before the provided timeout duration has expired.
Timeout(time::error::Elapsed),
/// Occurs when no TLS connector was specified, but the server requires a TLS connection.
TlsDisabled,
/// Occurs when trying to [`unsubscribe`](../struct.Client.html#method.unsubscribe) with
/// an unknown [`Sid`](../type.Sid.html)
UnknownSid(Sid),
}
impl Error {
/// Returns true if the error is a `NotConnected` error
pub fn not_connected(&self) -> bool {
matches!(self, Self::NotConnected)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::ExceedsMaxPayload { tried, limit } => {
write!(f, "'{}' exceeds max payload '{}'", tried, limit)
}
Error::InvalidAddress(address) => write!(f, "invalid address {:?}", address),
#[cfg(feature = "rustls-tls")]
Error::InvalidDnsName(input) => write!(f, "invalid DNS name '{}'", input),
Error::InvalidServerControl(line) => write!(f, "invalid control line {:?}", line),
Error::InvalidSubject(subject) => write!(f, "invalid subject {:?}", subject),
Error::InvalidTerminator(terminator) => {
write!(f, "invalid message terminator {:?}", terminator)
}
Error::Io(e) => write!(f, "{}", e),
#[cfg(feature = "tls")]
Error::Tls(e) => write!(f, "{}", e),
Error::NoResponse => write!(f, "no response"),
Error::NotConnected => write!(f, "not connected"),
Error::NotEnoughData => write!(f, "not enough data"),
Error::InvalidNetworkScheme(protocol) => write!(
f,
"invalid scheme '{}' only '{}' is supported",
protocol,
util::NATS_NETWORK_SCHEME
),
Error::UnknownSid(sid) => write!(f, "unknown sid '{}'", sid),
Error::Timeout(e) => write!(f, "{}", e),
Error::TlsDisabled => write!(f, "no TLS connector specified"),
}
}
}
impl std::error::Error for Error {}
impl From<tokio::time::error::Elapsed> for Error {
fn from(e: tokio::time::error::Elapsed) -> Self {
Error::Timeout(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
#[cfg(feature = "tls")]
impl From<TlsError> for Error {
fn from(e: TlsError) -> Self {
Error::Tls(e)
}
}
/// A `Result` that uses the `rants` [`Error`](enum.Error.html) type
pub type Result<T> = std::result::Result<T, Error>;