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
//! `nats` `Error` and `Result`
use std::{fmt, io};
use crate::{types::Sid, util};
/// 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 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 native tls errors
#[cfg(feature = "tls")]
NativeTls(native_tls::Error),
/// 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,
/// 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 {
if let Self::NotConnected = self {
true
} else {
false
}
}
}
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),
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::NativeTls(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::TlsDisabled => write!(f, "no TLS connector specified"),
}
}
}
impl std::error::Error for Error {}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
#[cfg(feature = "tls")]
impl From<native_tls::Error> for Error {
fn from(e: native_tls::Error) -> Self {
Error::NativeTls(e)
}
}
/// A `Result` that uses the `rants` [`Error`](enum.Error.html) type
pub type Result<T> = std::result::Result<T, Error>;