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#[derive(Debug, Fail)]
16pub enum NatsError {
17 #[fail(display = "CommandBuildError: {}", _0)]
19 CommandBuildError(String),
20 #[fail(display = "IOError: {:?}", _0)]
22 IOError(io::Error),
23 #[fail(display = "ServerDisconnected: {:?}", _0)]
26 ServerDisconnected(Option<io::Error>),
27 #[fail(display = "ProtocolError: {}", _0)]
29 ProtocolError(protocol::CommandError),
30 #[fail(display = "UTF8Error: {}", _0)]
32 UTF8Error(::std::string::FromUtf8Error),
33 #[fail(display = "TlsError: {}", _0)]
35 TlsError(::native_tls::Error),
36 #[fail(display = "TlsHostMissingError: Host is missing, can't verify server identity")]
38 TlsHostMissingError,
39 #[fail(display = "UrlParseError: {}", _0)]
41 UrlParseError(::url::ParseError),
42 #[fail(display = "AddrParseError: {}", _0)]
44 AddrParseError(::std::net::AddrParseError),
45 #[fail(display = "UriDNSResolveError: {:?}", _0)]
49 UriDNSResolveError(Option<io::Error>),
50 #[fail(display = "CannotReconnectToServer: cannot reconnect to server")]
52 CannotReconnectToServer,
53 #[fail(display = "InnerBrokenChain: the sender/receiver pair has been disconnected")]
55 InnerBrokenChain,
56 #[fail(
58 display = "MaxPayloadOverflow: the given payload exceeds the server setting (max_payload_size = {})",
59 _0
60 )]
61 MaxPayloadOverflow(u32),
62 #[fail(display = "GenericError: {}", _0)]
64 GenericError(String),
65 #[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);