karatelabs/karate · error · OAuth2Exception

Invalid callbackPort: " + portValue

Error message

Invalid callbackPort: " + portValue

What it means

Thrown by parsePort when the callbackPort config value is a String that cannot be parsed as an integer. The library accepts callbackPort as an Integer or a numeric String; anything else (e.g. "abc", "8080x", empty string, or a value with stray whitespace/symbols) is rejected.

Solutions

  1. Set callbackPort to a plain integer either as a number (8080) or a clean numeric string ("8080").
  2. Trim whitespace and remove any non-digit characters from the configured value.
  3. If the value comes from an env var, validate/parse it before passing it into the OAuth2 config.

Example fix

// before
config.callbackPort = " 8080 "; // unparseable with spaces in some paths / or "abc"
// after
config.callbackPort = 8080; // Integer, or "8080" trimmed
Defensive patterns

Strategy: validation

Validate before calling

Object v = config.get("callbackPort");
if (v instanceof String) {
    if (!((String) v).trim().matches("\\d+")) {
        throw new IllegalArgumentException("callbackPort must be numeric, got: " + v);
    }
}

Type guard

boolean isValidCallbackPort(Object v) {
    if (v instanceof Integer) return true;
    return v instanceof String && ((String) v).trim().matches("\\d+");
}

Try / catch

try {
    handler.token();
} catch (OAuth2Exception e) {
    if (e.getMessage().startsWith("Invalid callbackPort:")) {
        logger.error("Fix callbackPort to a plain integer: {}", e.getMessage());
    }
}

Prevention

When it happens

Trigger: callbackPort in the auth/config map is set to a String like "abc", "", "port 8080", or a number with trailing characters, so Integer.parseInt fails with NumberFormatException.

Common situations: Typo or stray character in a Karate config file; value read from an environment variable or properties file that contains whitespace or a comment; copy-paste including quotes or units ('8080px').

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/5b6fd5581e12d177. Report an issue: GitHub.

Appendix: source

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

            return parsePortList(callbackPorts);
        }

        // Default ports (Postman-style)
        return new int[] { 8080, 8888, 9090, 3000 };
    }

    /**
     * Parse a single port value
     */
    private int parsePort(Object portValue) {
        if (portValue instanceof Integer) {
            return (Integer) portValue;
        }
        if (portValue instanceof String) {
            try {
                return Integer.parseInt((String) portValue);
            } 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]);
                }

View on GitHub (pinned to a22eb90246)