crabka_client_core/
error.rs1use std::net::SocketAddr;
4use std::time::Duration;
5
6use thiserror::Error;
7
8#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum ClientError {
12 #[error("connect to {addr}: {source}")]
13 Connect {
14 addr: SocketAddr,
15 #[source]
16 source: std::io::Error,
17 },
18
19 #[error("connection closed")]
20 Disconnected,
21
22 #[error("request timed out after {0:?}")]
23 Timeout(Duration),
24
25 #[error(
26 "incompatible version: broker supports {broker_min}..={broker_max}, \
27 client wants {client_min}..={client_max} for api_key {api_key}"
28 )]
29 IncompatibleVersion {
30 api_key: i16,
31 broker_min: i16,
32 broker_max: i16,
33 client_min: i16,
34 client_max: i16,
35 },
36
37 #[error("protocol error from server: {error_code}")]
38 Server { error_code: i16 },
39
40 #[error("codec: {0}")]
41 Codec(#[from] crabka_protocol::ProtocolError),
42
43 #[error("I/O: {0}")]
44 Io(#[from] std::io::Error),
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 use assert2::assert;
51
52 #[test]
53 fn display_is_useful() {
54 let e = ClientError::Timeout(Duration::from_secs(5));
55 assert!(e.to_string() == "request timed out after 5s");
56 }
57
58 #[test]
59 fn incompatible_version_displays_full_range() {
60 let e = ClientError::IncompatibleVersion {
61 api_key: 0,
62 broker_min: 0,
63 broker_max: 5,
64 client_min: 7,
65 client_max: 10,
66 };
67 assert!(e.to_string().contains("api_key 0"));
68 assert!(e.to_string().contains("broker supports 0..=5"));
69 }
70}