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
use std::{fmt, io};
use crate::{types::Sid, util};
#[derive(Debug)]
pub enum Error {
ExceedsMaxPayload {
tried: usize,
limit: usize,
},
InvalidAddress(String),
InvalidNetworkScheme(String),
InvalidServerControl(String),
InvalidSubject(String),
InvalidTerminator(Vec<u8>),
Io(io::Error),
#[cfg(feature = "tls")]
NativeTls(native_tls::Error),
NoResponse,
NotConnected,
NotEnoughData,
TlsDisabled,
UnknownSid(Sid),
}
impl 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)
}
}
pub type Result<T> = std::result::Result<T, Error>;