karatelabs/karate · error · OAuth2Exception

Invalid callbackPort type: " + portValue.getClass()

Error message

Invalid callbackPort type: " + portValue.getClass()

What it means

Thrown by parsePort when the callbackPort value is neither an Integer nor a String — e.g. a Long, Double, Boolean, Map, or List. The handler only supports Integer and (parseable) String types for callbackPort and reports the actual offending class.

Solutions

  1. Cast or convert the value to Integer/String before assigning it to callbackPort.
  2. If the value is a Long/Double (e.g. from JS or YAML), convert with ((Number) value).intValue().
  3. Check the source file: unquote it to a plain integer, and ensure no decimal point is present.

Example fix

// before
config.callbackPort = 8080.0; // Double from JS/YAML
// after
config.callbackPort = (int) 8080; // or Integer.parseInt(String.valueOf(value))
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = config.get("callbackPort");
if (!(v instanceof Integer) && !(v instanceof String)) {
    throw new IllegalArgumentException("callbackPort must be Integer or String, got: " + v.getClass());
}

Type guard

boolean isAcceptedCallbackPortType(Object v) {
    return v instanceof Integer || v instanceof String;
}
// coerce numeric types first:
Integer coercePort(Object v) {
    if (v instanceof Number) return ((Number) v).intValue();
    if (v instanceof String) return Integer.valueOf(((String) v).trim());
    return null;
}

Try / catch

try {
    handler.token();
} catch (OAuth2Exception e) {
    if (e.getMessage().startsWith("Invalid callbackPort type:")) {
        logger.error("Coerce callbackPort to Integer/String: {}", e.getMessage());
    }
}

Prevention

When it happens

Trigger: callbackPort configured as a Long (common when read from JSON/YAML where the number overflows Integer parsing in the host tool), a Double (8080.0), or any non-scalar object.

Common situations: Config loaded from JSON/YAML where the value was quoted/typed differently; a JavaScript number arriving as Double from Karate's embedded JS; passing a config object field of the wrong type programmatically.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

        // 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]);
                }
            }
            return ports;
        }

View on GitHub (pinned to a22eb90246)