{"record":{"id":"da082e968f8bd4ca","repo":"apache/kafka","slug":"leading-or-trailing-whitespace-is-not-allowed","errorCode":null,"errorMessage":"Leading or trailing whitespace is not allowed.","messagePattern":"Leading or trailing whitespace is not allowed\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/admin/RaftVoterEndpoint.java","lineNumber":40,"sourceCode":"import java.util.Locale;\nimport java.util.Objects;\n\n/**\n * An endpoint for a raft quorum voter.\n */\n@InterfaceStability.Stable\n@InterfaceAudience.Public\npublic class RaftVoterEndpoint {\n    private final String listener;\n    private final String host;\n    private final int port;\n\n    private static String requireNonNullAllCapsNonEmpty(String input) {\n        if (input == null) {\n            throw new IllegalArgumentException(\"Null argument not allowed.\");\n        }\n        if (!input.trim().equals(input)) {\n            throw new IllegalArgumentException(\"Leading or trailing whitespace is not allowed.\");\n        }\n        if (input.isEmpty()) {\n            throw new IllegalArgumentException(\"Empty string is not allowed.\");\n        }\n        if (!input.toUpperCase(Locale.ROOT).equals(input)) {\n            throw new IllegalArgumentException(\"String must be UPPERCASE.\");\n        }\n        return input;\n    }\n\n    /**\n     * Create an endpoint for a metadata quorum voter.\n     *\n     * @param listener          The human-readable name for this endpoint. For example, CONTROLLER.\n     * @param host              The DNS hostname for this endpoint.\n     * @param port              The network port for this endpoint.\n     */\n    public RaftVoterEndpoint(","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/admin/RaftVoterEndpoint.java#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Trim the value before passing it: `listenerName.trim()`.","Audit the source config file for stray whitespace around the listener entry.","When reading from env vars, strip newlines explicitly."],"exampleFix":"// before\nnew RaftVoterEndpoint(rawListener, host, port);\n\n// after\nnew RaftVoterEndpoint(rawListener.trim(), host.trim(), port);","handlingStrategy":"validation","validationCode":"String trimmed = listenerName == null ? null : listenerName.trim();\nif (trimmed == null || trimmed.isEmpty() || !trimmed.equals(trimmed.toUpperCase(Locale.ROOT))) {\n    throw new IllegalArgumentException(\"Invalid listener name: \" + listenerName);\n}\nnew RaftVoterEndpoint(trimmed, host, port);","typeGuard":"static boolean isValidListenerName(String s) {\n    return s != null && s.trim().equals(s) && !s.isEmpty() && s.toUpperCase(Locale.ROOT).equals(s);\n}","tryCatchPattern":"try {\n    new RaftVoterEndpoint(listener, host, port);\n} catch (IllegalArgumentException e) {\n    log.error(\"Bad voter endpoint config: {}\", e.getMessage());\n}","preventionTips":["Trim user-supplied config values at load time.","Lint Kafka configs with a CI step that flags whitespace in identifiers.","Document listener names as uppercase identifiers in your project's ops guide."],"tags":["admin-client","kraft","raft-voter","validation","illegal-argument","whitespace"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}