1use std::fmt;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19#[non_exhaustive]
20pub enum TransportErrorKind {
21 Connect,
23 Protocol,
27 Http { status: u16 },
29 Closed,
31 Other,
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct TransportError {
39 pub kind: TransportErrorKind,
40 pub message: String,
41}
42
43impl TransportError {
44 pub fn new(kind: TransportErrorKind, message: impl Into<String>) -> Self {
45 Self {
46 kind,
47 message: message.into(),
48 }
49 }
50 pub fn connect(message: impl Into<String>) -> Self {
51 Self::new(TransportErrorKind::Connect, message)
52 }
53 pub fn protocol(message: impl Into<String>) -> Self {
54 Self::new(TransportErrorKind::Protocol, message)
55 }
56 pub fn http(status: u16, message: impl Into<String>) -> Self {
57 Self::new(TransportErrorKind::Http { status }, message)
58 }
59 pub fn closed(message: impl Into<String>) -> Self {
60 Self::new(TransportErrorKind::Closed, message)
61 }
62 pub fn other(message: impl Into<String>) -> Self {
63 Self::new(TransportErrorKind::Other, message)
64 }
65}
66
67impl fmt::Display for TransportError {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 match self.kind {
70 TransportErrorKind::Connect => write!(f, "transport connect: {}", self.message),
71 TransportErrorKind::Protocol => write!(f, "transport protocol: {}", self.message),
72 TransportErrorKind::Http { status } => {
73 write!(f, "transport http {}: {}", status, self.message)
74 }
75 TransportErrorKind::Closed => write!(f, "transport closed: {}", self.message),
76 TransportErrorKind::Other => write!(f, "transport: {}", self.message),
77 }
78 }
79}
80
81impl std::error::Error for TransportError {}
82
83#[derive(Debug, Clone, PartialEq)]
85#[non_exhaustive]
86pub enum ClientError {
87 Transport(TransportError),
89
90 Remote {
93 message: String,
94 code: Option<String>,
95 recoverable: bool,
96 },
97
98 Decode {
102 content_type: Option<String>,
104 message: String,
105 },
106
107 Encode {
111 param: &'static str,
113 message: String,
114 },
115}
116
117impl ClientError {
118 pub fn is_transport(&self) -> bool {
120 matches!(self, ClientError::Transport(_))
121 }
122 pub fn is_remote(&self) -> bool {
124 matches!(self, ClientError::Remote { .. })
125 }
126}
127
128impl From<TransportError> for ClientError {
129 fn from(e: TransportError) -> Self {
130 ClientError::Transport(e)
131 }
132}
133
134impl fmt::Display for ClientError {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 match self {
137 ClientError::Transport(e) => write!(f, "{e}"),
138 ClientError::Remote {
139 message,
140 code: Some(code),
141 ..
142 } => write!(f, "remote error [{code}]: {message}"),
143 ClientError::Remote { message, .. } => write!(f, "remote error: {message}"),
144 ClientError::Decode {
145 content_type: Some(ct),
146 message,
147 } => write!(f, "decode {ct}: {message}"),
148 ClientError::Decode { message, .. } => write!(f, "decode: {message}"),
149 ClientError::Encode { param, message } => {
150 write!(f, "encode param `{param}`: {message}")
151 }
152 }
153 }
154}
155
156impl std::error::Error for ClientError {}