Skip to main content

confluent_kafka/common/config/
config_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.common.config.ConfigException`.
16
17use std::fmt::Display;
18
19use crate::common::error::kafka_error_type;
20
21kafka_error_type! {
22    /// Thrown if the user supplies an invalid configuration
23    ///
24    /// # Translation notes
25    ///
26    /// No protocol code.
27    #[doc(alias = "org.apache.kafka.common.config.ConfigException")]
28    ConfigError,
29    extends: [
30        is_kafka_error,
31        is_config_error,
32    ],
33}
34
35impl ConfigError {
36    // Java's three `ConfigException` constructors — `ConfigException(String)`,
37    // `ConfigException(String, Object)` and
38    // `ConfigException(String, Object, String)` — have an EMPTY
39    // parameter-name intersection, and no constructor is no-arg. So under
40    // CLAUDE.md §2 nobody keeps the plain name, and the two below take their
41    // full parameter lists as the suffix.
42    //
43    // The `(String message)` form is the exception, and deliberately so: it is
44    // generated as `new` by `kafka_error_type!` above, whose single body
45    // declares 141 classes. Renaming it here would mean threading a per-class
46    // constructor name through the macro so that exactly one of the 141 spells
47    // it `with_message` — leaving `SomeError::new("msg")` valid or not depending
48    // on a class the call site cannot see. The macro's uniformity is worth more
49    // than this one suffix; see the note on `message_only_error!` for the same
50    // trade-off.
51
52    /// Creates a config error naming the offending value and configuration key:
53    /// `"Invalid value {value} for configuration {name}"`.
54    ///
55    /// # Arguments
56    ///
57    /// - `name` - the configuration key
58    /// - `value` - the invalid value
59    #[doc(alias = "org.apache.kafka.common.config.ConfigException#ConfigException")]
60    pub fn with_name_value(name: impl Display, value: impl Display) -> Self {
61        Self::new(format!("Invalid value {value} for configuration {name}"))
62    }
63
64    /// Creates a config error naming the value, key, and a detail message:
65    /// `"Invalid value {value} for configuration {name}: {message}"`.
66    ///
67    /// # Arguments
68    ///
69    /// - `name` - the configuration key
70    /// - `value` - the invalid value
71    /// - `message` - why the value is invalid
72    #[doc(alias = "org.apache.kafka.common.config.ConfigException#ConfigException")]
73    pub fn with_name_value_message(name: impl Display, value: impl Display, message: impl Display) -> Self {
74        Self::new(format!("Invalid value {value} for configuration {name}: {message}"))
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::ConfigError;
81    use crate::common::Error;
82
83    /// The two structured constructors reproduce Java's
84    /// `ConfigException(name, value)` and `ConfigException(name, value, message)`
85    /// message formats byte-for-byte.
86    #[test]
87    fn message_matches_java_config_error_format() {
88        assert_eq!(
89            ConfigError::with_name_value("group.protocol", "bad").message(),
90            "Invalid value bad for configuration group.protocol"
91        );
92        assert_eq!(
93            ConfigError::with_name_value_message("max.poll.records", 0, "Value must be at least 1").message(),
94            "Invalid value 0 for configuration max.poll.records: Value must be at least 1"
95        );
96    }
97
98    /// `ConfigException extends KafkaException`, so a config error is a Kafka
99    /// error — unlike a `java.lang.IllegalArgumentException`. This is the whole
100    /// point of routing config validation through `ConfigError` (Critic finding 7).
101    #[test]
102    fn config_error_is_a_kafka_error() {
103        let e = Error::config_name_value("group.protocol", "bad");
104        assert!(e.is_kafka_error());
105        assert!(!e.is_api_error(), "a config error is a Kafka error directly, not an API error");
106        assert!(matches!(e, Error::Config(_)));
107    }
108}