Skip to main content

ruststream_rdkafka/
error.rs

1//! The crate error type shared by the broker, publishers, and subscribers.
2
3use std::error::Error as StdError;
4
5use thiserror::Error;
6
7/// Errors returned by [`KafkaBroker`](crate::KafkaBroker) and the types it hands out.
8///
9/// Underlying [`rdkafka`](https://docs.rs/rdkafka) errors are boxed as sources so the client
10/// library does not leak into this crate's public API surface.
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum KafkaError {
14    /// Creating a client failed or the cluster was unreachable during the connect probe.
15    #[error("kafka connection error: {0}")]
16    Connect(#[source] Box<dyn StdError + Send + Sync>),
17
18    /// Publishing a message failed or the broker did not confirm its delivery.
19    #[error("kafka publish error: {0}")]
20    Publish(#[source] Box<dyn StdError + Send + Sync>),
21
22    /// Creating a consumer or subscribing it to its topic failed.
23    #[error("kafka subscribe error: {0}")]
24    Subscribe(#[source] Box<dyn StdError + Send + Sync>),
25
26    /// Receiving a delivery from an open consumer failed.
27    #[error("kafka consume error: {0}")]
28    Consume(#[source] Box<dyn StdError + Send + Sync>),
29
30    /// An operation needed the live connection before `Broker::connect` resolved it.
31    ///
32    /// The runtime connects the broker once at startup; a publisher handed out earlier resolves
33    /// the shared connection on first use. Seeing this error means the operation ran before
34    /// `connect` completed.
35    #[error("kafka broker is not connected; `Broker::connect` must complete first")]
36    NotConnected,
37
38    /// The requested combination of options cannot be executed.
39    ///
40    /// The message names the offending option and the remediation.
41    #[error("invalid options: {0}")]
42    InvalidOptions(String),
43}
44
45impl KafkaError {
46    pub(crate) fn connect(err: rdkafka::error::KafkaError) -> Self {
47        Self::Connect(Box::new(err))
48    }
49
50    pub(crate) fn publish(err: rdkafka::error::KafkaError) -> Self {
51        Self::Publish(Box::new(err))
52    }
53
54    pub(crate) fn subscribe(err: rdkafka::error::KafkaError) -> Self {
55        Self::Subscribe(Box::new(err))
56    }
57
58    pub(crate) fn consume(err: rdkafka::error::KafkaError) -> Self {
59        Self::Consume(Box::new(err))
60    }
61}