karatelabs/karate · error · OAuth2Exception

Invalid port in callbackPorts: " + parts[i]

Error message

Invalid port in callbackPorts: " + parts[i]

What it means

Thrown by parsePortList when an element of the comma-separated callbackPorts string fails Integer.parseInt. Each comma-separated token must be a valid integer port; the message identifies the exact offending token.

Solutions

  1. Correct the callbackPorts string so every comma-separated token is a plain integer, e.g. "9090,9091,9092".
  2. Remove empty segments (double/trailing commas) from the list.
  3. Trim whitespace around each port (the code trims, but stray letters/symbols are not tolerated).

Example fix

// before
.callbackPorts("9090,,9091") // empty segment
// after
.callbackPorts("9090,9091")
Defensive patterns

Strategy: validation

Validate before calling

Object v = config.get("callbackPorts");
if (v instanceof String) {
    for (String part : ((String) v).split(",")) {
        if (!part.trim().matches("\\d+")) {
            throw new IllegalArgumentException("Bad port token in callbackPorts: '" + part + "'");
        }
    }
}

Type guard

boolean isValidCallbackPorts(Object v) {
    if (!(v instanceof String)) return false;
    return java.util.Arrays.stream(((String) v).split(","))
        .allMatch(s -> s.trim().matches("\\d+"));
}

Try / catch

try {
    handler.token();
} catch (OAuth2Exception e) {
    if (e.getMessage().startsWith("Invalid port in callbackPorts:")) {
        logger.error("Every comma-separated token must be an integer: {}", e.getMessage());
    }
}

Prevention

When it happens

Trigger: callbackPorts set to a String like "9090, abc" or "9090, ,9091" — one of the split/trimmed parts is not a valid integer.

Common situations: Typo in a port list; accidental double comma leaving an empty segment; trailing comma producing an empty last part; ports pasted with stray characters or units.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/f39359f911240872. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/http/AuthorizationCodeAuthHandler.java:299

            } catch (NumberFormatException e) {
                throw new OAuth2Exception("Invalid callbackPort: " + portValue);
            }
        }
        throw new OAuth2Exception("Invalid callbackPort type: " + portValue.getClass());
    }

    /**
     * Parse comma-separated port list
     */
    private int[] parsePortList(Object portsValue) {
        if (portsValue instanceof String) {
            String[] parts = ((String) portsValue).split(",");
            int[] ports = new int[parts.length];
            for (int i = 0; i < parts.length; i++) {
                try {
                    ports[i] = Integer.parseInt(parts[i].trim());
                } catch (NumberFormatException e) {
                    throw new OAuth2Exception("Invalid port in callbackPorts: " + parts[i]);
                }
            }
            return ports;
        }
        throw new OAuth2Exception("Invalid callbackPorts type: " + portsValue.getClass());
    }

    private String generateState() {
        // Simple state generation - could be more sophisticated
        return java.util.UUID.randomUUID().toString();
    }

    private String urlEncode(String value) {
        try {
            return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            throw new OAuth2Exception("URL encoding failed", e);
        }

View on GitHub (pinned to a22eb90246)