useceleris_client/errors.rs
1use std::fmt;
2
3/// A failure the SDK detected, or an error frame the server sent.
4///
5/// The SDK's own variants are a closed set and carry a message that names what
6/// failed and the rule or limit it broke. Such a message never carries input
7/// values, credentials or bytes the server sent. Match on the variant, never on
8/// the message.
9///
10/// There is no authentication variant: a handshake that the server refuses for
11/// its credentials cannot be told apart from a network failure (DEV-02), so it
12/// is reported as [`CelerisError::Transport`].
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum CelerisError {
16 /// Invalid input: options, identifiers, payload helpers, or credentials that
17 /// a provider returned.
18 Configuration(String),
19
20 /// A connection attempt or a presence query ran past its deadline.
21 Timeout(String),
22
23 /// An operation stopped because the channel closed or a newer attempt
24 /// started.
25 Cancelled(String),
26
27 /// A failed connection: the credential provider failed, the handshake was
28 /// refused, or the socket broke.
29 Transport(String),
30
31 /// An operation needs a connection that the channel does not have.
32 NotConnected(String),
33
34 /// The publish queue is full, or sending is paused after a rate limit.
35 /// Retry once it drains.
36 Backpressure(String),
37
38 /// A second connect or presence query while the first is still running.
39 OperationInProgress(String),
40
41 /// A publish may or may not have left the socket. It is never resent
42 /// automatically.
43 DeliveryUnknown(String),
44
45 /// A server message could not be decoded. The message is dropped and the
46 /// connection stays up (DECODE-01).
47 Protocol {
48 message: String,
49
50 /// A field name that this crate chose.
51 field: &'static str,
52
53 /// A zero-based byte offset into the server message.
54 offset: usize,
55 },
56
57 /// An error frame from the server, every field exactly as sent (ERR-01).
58 Server(ServerError),
59} // end enum CelerisError
60
61impl fmt::Display for CelerisError {
62 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match self {
64 CelerisError::Configuration(message)
65 | CelerisError::Timeout(message)
66 | CelerisError::Cancelled(message)
67 | CelerisError::Transport(message)
68 | CelerisError::NotConnected(message)
69 | CelerisError::Backpressure(message)
70 | CelerisError::OperationInProgress(message)
71 | CelerisError::DeliveryUnknown(message) => formatter.write_str(message),
72 CelerisError::Protocol {
73 message,
74 field,
75 offset,
76 } => write!(formatter, "{message} Field: {field}, byte offset {offset}."),
77 CelerisError::Server(server_error) => server_error.fmt(formatter),
78 }
79 } // end method fmt
80} // end impl Display for CelerisError
81
82impl std::error::Error for CelerisError {}
83
84/// The kind of an error frame that the server sent. The set stays open: a type
85/// that a newer server adds still reaches the caller as
86/// [`ServerErrorType::Other`] (ERR-01).
87#[derive(Clone, Debug, PartialEq, Eq, Hash)]
88#[non_exhaustive]
89pub enum ServerErrorType {
90 /// The server could not parse a command.
91 ParserError,
92
93 /// The server failed to send.
94 SendError,
95
96 /// The token lacks access to what the command tried.
97 PermissionDeniedError,
98
99 /// A message limit was exceeded.
100 RateLimitError,
101
102 /// An encoded publish command exceeded the plan's cap, or a message
103 /// exceeded the 2 MiB transport ceiling.
104 MessageSizeLimitError,
105
106 /// A server-side fault that the client did not cause.
107 InternalError,
108
109 /// A type that this version does not know, exactly as sent.
110 Other(String),
111} // end enum ServerErrorType
112
113impl ServerErrorType {
114 /// The type as the server wrote it.
115 pub fn as_str(&self) -> &str {
116 match self {
117 ServerErrorType::ParserError => "ParserError",
118 ServerErrorType::SendError => "SendError",
119 ServerErrorType::PermissionDeniedError => "PermissionDeniedError",
120 ServerErrorType::RateLimitError => "RateLimitError",
121 ServerErrorType::MessageSizeLimitError => "MessageSizeLimitError",
122 ServerErrorType::InternalError => "InternalError",
123 ServerErrorType::Other(name) => name,
124 }
125 } // end method as_str
126
127 pub(crate) fn from_wire(name: String) -> ServerErrorType {
128 match name.as_str() {
129 "ParserError" => ServerErrorType::ParserError,
130 "SendError" => ServerErrorType::SendError,
131 "PermissionDeniedError" => ServerErrorType::PermissionDeniedError,
132 "RateLimitError" => ServerErrorType::RateLimitError,
133 "MessageSizeLimitError" => ServerErrorType::MessageSizeLimitError,
134 "InternalError" => ServerErrorType::InternalError,
135 _ => ServerErrorType::Other(name),
136 }
137 } // end fn from_wire
138} // end impl ServerErrorType
139
140/// What an error frame's resource carries: whatever its type and sub type
141/// define, such as the segment that a denial refers to.
142#[derive(Clone, Debug, PartialEq, Eq)]
143pub enum ServerErrorResource {
144 Null,
145 Text(String),
146 Integer32(i32),
147 Integer64(i64),
148 Array(Vec<ServerErrorResource>),
149} // end enum ServerErrorResource
150
151/// An error frame from the server, every field exactly as sent (ERR-01). The
152/// server keeps the connection open after it sends one.
153#[derive(Clone, Debug, PartialEq, Eq)]
154pub struct ServerError {
155 pub error_type: ServerErrorType,
156
157 /// The command that the error answers, such as `"SUB"` or `"PRES_LIST"`,
158 /// or `None` when the server named none.
159 pub sub_type: Option<String>,
160
161 /// The server's own text, for people to read. Never parse it.
162 pub message: String,
163
164 pub resource: ServerErrorResource,
165} // end struct ServerError
166
167impl fmt::Display for ServerError {
168 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
169 write!(formatter, "{}: {}", self.error_type.as_str(), self.message)
170 } // end method fmt
171} // end impl Display for ServerError
172
173/// Names every field that failed and the rule it broke, never the value:
174/// "Invalid client options. credential_provider: Required."
175pub(crate) fn configuration_error(subject: &str, failures: &[String]) -> CelerisError {
176 CelerisError::Configuration(format!("Invalid {subject}. {}", failures.join(" ")))
177} // end fn configuration_error
178
179/// Describes one failed field. An empty path describes the value as a whole.
180pub(crate) fn failure(path: &str, rule: &str) -> String {
181 if path.is_empty() {
182 return format!("{rule}.");
183 }
184
185 format!("{path}: {rule}.")
186} // end fn failure
187
188pub(crate) fn protocol_error(message: &str, field: &'static str, offset: usize) -> CelerisError {
189 CelerisError::Protocol {
190 message: message.to_owned(),
191 field,
192 offset,
193 }
194} // end fn protocol_error