krabka_client_core/
error.rs1use std::{io, net::SocketAddr};
4
5use krabka_units::{Time, fmt::Human as _};
6use thiserror::Error;
7
8use crate::sasl;
9
10#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum ClientError {
14 #[error("connect to {addr}: {source}")]
15 Connect {
16 addr: SocketAddr,
17 #[source]
18 source: io::Error,
19 },
20
21 #[error("TLS handshake with {addr}: {source}")]
26 Tls {
27 addr: SocketAddr,
28 #[source]
29 source: io::Error,
30 },
31
32 #[error("SASL authentication with {addr}: {source}")]
39 Sasl {
40 addr: SocketAddr,
41 #[source]
42 source: sasl::OutboundSaslError,
43 },
44
45 #[error("authentication with {addr} failed: {source}")]
51 Authentication {
52 addr: SocketAddr,
53 #[source]
54 source: AuthenticationError,
55 },
56
57 #[error("connection closed")]
58 Disconnected,
59
60 #[error("invalid client configuration: {0}")]
61 InvalidConfig(String),
62
63 #[error("invalid argument: {0}")]
66 InvalidArgument(String),
67
68 #[error("Telemetry is not enabled. Set config `enable.metrics.push` to `true`.")]
72 TelemetryDisabled,
73
74 #[error("request timed out after {}", .0.human())]
75 Timeout(Time),
76
77 #[error(
78 "incompatible version: broker supports {broker_min}..={broker_max}, \
79 client wants {client_min}..={client_max} for api_key {api_key}"
80 )]
81 IncompatibleVersion {
82 api_key: i16,
83 broker_min: i16,
84 broker_max: i16,
85 client_min: i16,
86 client_max: i16,
87 },
88
89 #[error("protocol error from server: {error_code}")]
90 Server { error_code: i16 },
91
92 #[error("FindCoordinator returned no entry for key {key:?}")]
93 NoCoordinator { key: String },
94
95 #[error("codec: {0}")]
96 Codec(#[from] krabka_protocol::ProtocolError),
97
98 #[error("I/O: {0}")]
99 Io(#[from] io::Error),
100}
101
102impl ClientError {
103 #[must_use]
106 pub const fn is_authentication_failure(&self) -> bool {
107 matches!(self, Self::Authentication { .. })
108 }
109}
110
111#[derive(Debug, Error)]
114pub enum AuthenticationError {
115 #[error("TLS handshake failed: {0}")]
119 Tls(#[source] io::Error),
120
121 #[error(transparent)]
123 Sasl(#[from] sasl::SaslAuthenticationError),
124}
125
126#[cfg(test)]
127mod tests {
128 use assert2::assert;
129
130 use super::*;
131
132 #[test]
133 fn display_is_useful() {
134 let e = ClientError::Timeout(krabka_units::secs(5));
135 assert!(e.to_string() == "request timed out after 5s");
136 }
137
138 #[test]
139 fn incompatible_version_displays_full_range() {
140 let e = ClientError::IncompatibleVersion {
141 api_key: 0,
142 broker_min: 0,
143 broker_max: 5,
144 client_min: 7,
145 client_max: 10,
146 };
147 assert!(e.to_string().contains("api_key 0"));
148 assert!(e.to_string().contains("broker supports 0..=5"));
149 }
150}