SonarSource/sonarqube · error · IllegalArgumentException

format("' ' value ' ' cannot be parsed as an integer", key…

Error message

format("'%s' value '%s' cannot be parsed as an integer", key, value)

What it means

validateAsNumeric backs valueAsInt(key): it parses the parameter string with Integer.parseInt and wraps NumberFormatException in an IllegalArgumentException naming the parameter key and offending value. The library throws it so callers get a clear, request-scoped message instead of a raw NumberFormatException.

Solutions

  1. Validate the parameter client-side before sending, ensuring it matches ^-?\d+$ and fits in an int.
  2. Call valueAsLong(key) instead if the value may exceed Integer.MAX_VALUE.
  3. Catch the IllegalArgumentException in the request handler and return a 400 response with a friendly message.

Example fix

// before
int page = request.valueAsInt("page"); // throws for "1.5"

// after
String raw = request.value("page");
int page = raw != null && raw.matches("-?\\d+") ? Integer.parseInt(raw) : 1;
Defensive patterns

Strategy: try-catch

Validate before calling

// Java
String raw = request.value(key);
if (raw != null && !raw.matches("-?\\d+")) {
  throw new IllegalArgumentException("Not an integer: " + key + "=" + raw);
}

Try / catch

// Java
try {
  int n = request.valueAsInt(key);
} catch (IllegalArgumentException e) {
  // return 400 with a user-friendly message
}

Prevention

When it happens

Trigger: Calling request.valueAsInt("key") when the client sent a value that is not a 32-bit signed integer (e.g. "abc", "1.5", "", or a number above 2147483647).

Common situations: Users typing non-numeric values into API query parameters; JavaScript clients sending float or string values where an int is required; parameter exceeding Integer.MAX_VALUE (use valueAsLong instead).

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/de75ad4936054dd5. Report an issue: GitHub.

Appendix: source

Thrown at sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java:244

    if (maximumValue == null) {
      return;
    }
    int valueAsInt = validateAsNumeric(key, value);
    checkArgument(valueAsInt <= maximumValue, "'%s' value (%s) must be less than %s", key, valueAsInt, maximumValue);
  }

  private static void validateRequiredValue(String key, WebService.Param definition, @Nullable String value) {
    boolean required = definition.isRequired();
    if (required) {
      checkArgument(value != null, format(MSG_PARAMETER_MISSING, key));
    }
  }

  private static int validateAsNumeric(String key, String value) {
    try {
      return Integer.parseInt(value);
    } catch (NumberFormatException exception) {
      throw new IllegalArgumentException(format("'%s' value '%s' cannot be parsed as an integer", key, value), exception);
    }
  }

}

View on GitHub (pinned to 184c821202)