apache/kafka · error · IllegalArgumentException

Leading or trailing whitespace is not allowed.

Error message

Leading or trailing whitespace is not allowed.

What it means

Thrown by RaftVoterEndpoint.requireNonNullAllCapsNonEmpty when the listener string has leading or trailing whitespace (input.trim() != input). Whitespace in a listener name would silently fail to match any configured listener, so the validator rejects it eagerly rather than producing a confusing connection failure later.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/admin/RaftVoterEndpoint.java:40

import java.util.Locale;
import java.util.Objects;

/**
 * An endpoint for a raft quorum voter.
 */
@InterfaceStability.Stable
@InterfaceAudience.Public
public class RaftVoterEndpoint {
    private final String listener;
    private final String host;
    private final int port;

    private static String requireNonNullAllCapsNonEmpty(String input) {
        if (input == null) {
            throw new IllegalArgumentException("Null argument not allowed.");
        }
        if (!input.trim().equals(input)) {
            throw new IllegalArgumentException("Leading or trailing whitespace is not allowed.");
        }
        if (input.isEmpty()) {
            throw new IllegalArgumentException("Empty string is not allowed.");
        }
        if (!input.toUpperCase(Locale.ROOT).equals(input)) {
            throw new IllegalArgumentException("String must be UPPERCASE.");
        }
        return input;
    }

    /**
     * Create an endpoint for a metadata quorum voter.
     *
     * @param listener          The human-readable name for this endpoint. For example, CONTROLLER.
     * @param host              The DNS hostname for this endpoint.
     * @param port              The network port for this endpoint.
     */
    public RaftVoterEndpoint(

View on GitHub (pinned to 996fb4585a)

Solutions

  1. Trim the value before passing it: `listenerName.trim()`.
  2. Audit the source config file for stray whitespace around the listener entry.
  3. When reading from env vars, strip newlines explicitly.

Example fix

// before
new RaftVoterEndpoint(rawListener, host, port);

// after
new RaftVoterEndpoint(rawListener.trim(), host.trim(), port);
Defensive patterns

Strategy: validation

Validate before calling

String trimmed = listenerName == null ? null : listenerName.trim();
if (trimmed == null || trimmed.isEmpty() || !trimmed.equals(trimmed.toUpperCase(Locale.ROOT))) {
    throw new IllegalArgumentException("Invalid listener name: " + listenerName);
}
new RaftVoterEndpoint(trimmed, host, port);

Type guard

static boolean isValidListenerName(String s) {
    return s != null && s.trim().equals(s) && !s.isEmpty() && s.toUpperCase(Locale.ROOT).equals(s);
}

Try / catch

try {
    new RaftVoterEndpoint(listener, host, port);
} catch (IllegalArgumentException e) {
    log.error("Bad voter endpoint config: {}", e.getMessage());
}

Prevention

When it happens

Trigger: Constructing `new RaftVoterEndpoint(" CONTROLLER", host, port)` or any listener value copied from a config file/JSON with stray spaces. Also triggered by line breaks embedded in YAML/properties values.

Common situations: Hand-edited JSON/properties files with trailing spaces; values loaded from environment variables that include a newline; copy-paste from documentation that introduced a non-breaking space.

Related errors


AI-assisted analysis of apache/kafka@996fb4585a (2026-08-11). Data as JSON: /api/errors/da082e968f8bd4ca. Report an issue: GitHub.