Skip to main content

kafka_client_engine/consumer/group_close/
error.rs

1//! Stable terminal error vocabulary for accepted explicit close.
2
3use super::super::group::{
4    GroupConsumerCloseObservationError, GroupConsumerCloseTerminalFailure,
5    GroupConsumerCloseTerminalFailureKind, GroupConsumerShardLockError,
6};
7
8/// Stable terminal observation failure for one accepted close.
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub struct GroupConsumerCloseError {
11    kind: GroupConsumerCloseErrorKind,
12    broker_code: Option<i16>,
13}
14
15impl GroupConsumerCloseError {
16    /// Returns the stable terminal observation failure category.
17    pub const fn kind(&self) -> GroupConsumerCloseErrorKind {
18        self.kind
19    }
20
21    /// Returns Kafka's exact signed `LeaveGroup` error code when supplied.
22    pub const fn broker_code(&self) -> Option<i16> {
23        self.broker_code
24    }
25}
26
27/// Stable category for accepted close terminal observation failure.
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29pub enum GroupConsumerCloseErrorKind {
30    /// The original close deadline elapsed.
31    DeadlineElapsed,
32    /// The driver rejected bounded request ownership.
33    DriverRejected,
34    /// Authentication failed before the broker accepted `LeaveGroup`.
35    Authentication,
36    /// Transport ownership ended without a valid broker terminal.
37    Transport,
38    /// Kafka returned a nonzero signed `LeaveGroup` error code.
39    BrokerRejected,
40    /// The broker and client share no compatible classic-member version.
41    Compatibility,
42    /// The correlated response was structurally or semantically invalid.
43    InvalidResponse,
44    /// The response exceeded a configured decode bound.
45    ResponseTooLarge,
46    /// Hosted group execution retained a terminal fault.
47    HostUnavailable,
48    /// Notification or registry ownership violated an invariant.
49    InternalInvariant,
50}
51
52impl core::fmt::Display for GroupConsumerCloseError {
53    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54        write!(
55            formatter,
56            "classic-group consumer close observation failed: {:?}",
57            self.kind
58        )
59    }
60}
61
62impl std::error::Error for GroupConsumerCloseError {}
63
64pub(super) const fn observation_error(
65    error: GroupConsumerCloseObservationError,
66) -> GroupConsumerCloseError {
67    match error {
68        GroupConsumerCloseObservationError::Lock(GroupConsumerShardLockError::Contended) => {
69            internal_error()
70        }
71        GroupConsumerCloseObservationError::Lock(GroupConsumerShardLockError::Poisoned) => {
72            host_unavailable_error()
73        }
74    }
75}
76
77pub(super) const fn internal_error() -> GroupConsumerCloseError {
78    GroupConsumerCloseError {
79        kind: GroupConsumerCloseErrorKind::InternalInvariant,
80        broker_code: None,
81    }
82}
83
84pub(super) const fn host_unavailable_error() -> GroupConsumerCloseError {
85    GroupConsumerCloseError {
86        kind: GroupConsumerCloseErrorKind::HostUnavailable,
87        broker_code: None,
88    }
89}
90
91pub(super) const fn terminal_error(
92    failure: GroupConsumerCloseTerminalFailure,
93) -> GroupConsumerCloseError {
94    let kind = match failure.kind {
95        GroupConsumerCloseTerminalFailureKind::DeadlineElapsed => {
96            GroupConsumerCloseErrorKind::DeadlineElapsed
97        }
98        GroupConsumerCloseTerminalFailureKind::DriverRejected => {
99            GroupConsumerCloseErrorKind::DriverRejected
100        }
101        GroupConsumerCloseTerminalFailureKind::Authentication => {
102            GroupConsumerCloseErrorKind::Authentication
103        }
104        GroupConsumerCloseTerminalFailureKind::Transport
105        | GroupConsumerCloseTerminalFailureKind::DriverShutdown => {
106            GroupConsumerCloseErrorKind::Transport
107        }
108        GroupConsumerCloseTerminalFailureKind::BrokerRejected => {
109            GroupConsumerCloseErrorKind::BrokerRejected
110        }
111        GroupConsumerCloseTerminalFailureKind::Compatibility => {
112            GroupConsumerCloseErrorKind::Compatibility
113        }
114        GroupConsumerCloseTerminalFailureKind::InvalidResponse => {
115            GroupConsumerCloseErrorKind::InvalidResponse
116        }
117        GroupConsumerCloseTerminalFailureKind::ResponseTooLarge => {
118            GroupConsumerCloseErrorKind::ResponseTooLarge
119        }
120    };
121    GroupConsumerCloseError {
122        kind,
123        broker_code: failure.broker_code,
124    }
125}