confluent_kafka/consumer/consumer_commit_failed_error.rs
1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Translated from `org.apache.kafka.clients.consumer.CommitFailedException`.
16
17use std::fmt;
18
19use crate::common::Error;
20use crate::common::error::{ErrorCode, ErrorHierarchy, ErrorMessage, ErrorSource};
21
22/// This error is returned when an offset commit with
23/// [`Consumer::commit_sync`](crate::consumer::Consumer::commit_sync) fails with
24/// an unrecoverable error. This error is generated on the client side,
25/// typically when a group rebalance completes before the commit could be
26/// successfully applied. In this case, the commit cannot generally be retried
27/// because some of the partitions may have already been assigned to another
28/// member in the group.
29// Java `extends` chain: `CommitFailedException` -> `KafkaException`. It is NOT
30// an `ApiException`, unlike its sibling `ConsumerRetriableCommitFailedError`,
31// and has no entry in `Errors`, so it carries no protocol code.
32#[derive(Clone, Debug)]
33#[doc(alias = "org.apache.kafka.clients.consumer.CommitFailedException")]
34pub struct ConsumerCommitFailedError {
35 message: String,
36}
37
38impl ConsumerCommitFailedError {
39 /// Default message used by Java's `CommitFailedException` no-arg constructor.
40 ///
41 /// Kept as a constant so tests can assert exact equality.
42 pub(crate) const CONSUMER_COMMIT_FAILED_DEFAULT_MESSAGE: &str = "Commit cannot be completed since the group has already rebalanced and assigned the partitions to another member. This means that the time between subsequent calls to poll() was longer than the configured max.poll.interval.ms, which typically implies that the poll loop is spending too much time message processing. You can address this either by increasing max.poll.interval.ms or by reducing the maximum size of batches returned in poll() with max.poll.records.";
43
44 /// Create the error with the given message.
45 ///
46 /// # Arguments
47 ///
48 /// - `message` - The error message
49 #[doc(alias = "org.apache.kafka.clients.consumer.CommitFailedException#CommitFailedException")]
50 pub fn new(message: impl Into<String>) -> Self {
51 Self { message: message.into() }
52 }
53
54 /// Create the error with Java's no-arg constructor message.
55 #[doc(alias = "org.apache.kafka.clients.consumer.CommitFailedException#CommitFailedException")]
56 pub fn with_default_message() -> Self {
57 Self::new(ConsumerCommitFailedError::CONSUMER_COMMIT_FAILED_DEFAULT_MESSAGE)
58 }
59
60 /// Returns the error message.
61 // Java's Throwable.getMessage, a JDK method
62 #[doc(alias = "rust-only")]
63 pub fn message(&self) -> &str {
64 &self.message
65 }
66}
67
68impl fmt::Display for ConsumerCommitFailedError {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "ConsumerCommitFailedError: {}", self.message)
71 }
72}
73
74impl ErrorMessage for ConsumerCommitFailedError {
75 fn message(&self) -> &str {
76 &self.message
77 }
78}
79
80impl ErrorCode for ConsumerCommitFailedError {}
81
82impl ErrorHierarchy for ConsumerCommitFailedError {
83 fn is_kafka_error(&self) -> bool {
84 true
85 }
86 fn is_consumer_commit_failed_error(&self) -> bool {
87 true
88 }
89}
90
91impl ErrorSource for ConsumerCommitFailedError {
92 // `CommitFailedException` exposes no `Throwable cause` constructor, so its cause is
93 // always null in Java; the trait default (`None`) is that answer.
94}
95
96impl ConsumerCommitFailedError {
97 /// Returns the cause of this error: always `None`, as no constructor takes
98 /// one.
99 // An inherent method, so it shadows both `ErrorSource::source` and
100 // `std::error::Error::source`, keeping `x.source()` unambiguous and typed.
101 // Java's Throwable.getCause, a JDK method
102 #[doc(alias = "rust-only")]
103 pub fn source(&self) -> Option<&Error> {
104 None
105 }
106}
107
108impl std::error::Error for ConsumerCommitFailedError {
109 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
110 Option::<&Error>::None.map(|e| e as &(dyn std::error::Error + 'static))
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 /// Recovered from `master:src/consumer/errors.rs`
119 /// (`test_commit_failed_default_message`), which was dropped when that file
120 /// was split into one file per error class.
121 ///
122 /// Java: `CommitFailedException()` uses the long default message
123 /// (`org.apache.kafka.clients.consumer.CommitFailedException#CommitFailedException()`).
124 #[test]
125 fn test_commit_failed_default_message() {
126 let e = ConsumerCommitFailedError::with_default_message();
127 assert_eq!(e.message(), ConsumerCommitFailedError::CONSUMER_COMMIT_FAILED_DEFAULT_MESSAGE);
128 assert_eq!(
129 e.to_string(),
130 format!(
131 "ConsumerCommitFailedError: {}",
132 ConsumerCommitFailedError::CONSUMER_COMMIT_FAILED_DEFAULT_MESSAGE
133 )
134 );
135 }
136
137 /// Recovered from `master:src/consumer/errors.rs`
138 /// (`test_commit_failed_custom_message`). Java:
139 /// `CommitFailedException(String message)`.
140 #[test]
141 fn test_commit_failed_custom_message() {
142 let e = ConsumerCommitFailedError::new("custom");
143 assert_eq!(e.message(), "custom");
144 assert_eq!(e.to_string(), "ConsumerCommitFailedError: custom");
145 }
146
147 /// Recovered from `master:src/consumer/errors.rs`
148 /// (`test_commit_failed_into_kafka_error_is_not_retriable`), retargeted from
149 /// the removed `ConsumerError -> KafkaError` conversion onto the [`Error`]
150 /// variant that replaced it.
151 ///
152 /// `CommitFailedException extends KafkaException` directly
153 /// (`org.apache.kafka.clients.consumer.CommitFailedException`), so it is a `KafkaException` but NOT an
154 /// `ApiException` and NOT retriable — the exact opposite of its
155 /// [`ConsumerRetriableCommitFailedError`](super::ConsumerRetriableCommitFailedError)
156 /// sibling.
157 #[test]
158 fn test_commit_failed_error_hierarchy_is_not_retriable() {
159 let e = Error::ConsumerCommitFailed(ConsumerCommitFailedError::new("x"));
160 assert!(e.is_kafka_error());
161 assert!(!e.is_api_error());
162 assert!(!e.is_retriable_error());
163 // `CommitFailedException` has no entry in `Errors`, so it carries no
164 // protocol code of its own.
165 assert!(e.source().is_none());
166 }
167}