{"record":{"id":"de75ad4936054dd5","repo":"SonarSource/sonarqube","slug":"format-s-value-s-cannot-be-parsed-as-an-int","errorCode":null,"errorMessage":"format(\"'%s' value '%s' cannot be parsed as an integer\", key, value)","messagePattern":"format\\(\"'(.+?)' value '(.+?)' cannot be parsed as an integer\", key, value\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java","lineNumber":244,"sourceCode":"    if (maximumValue == null) {\n      return;\n    }\n    int valueAsInt = validateAsNumeric(key, value);\n    checkArgument(valueAsInt <= maximumValue, \"'%s' value (%s) must be less than %s\", key, valueAsInt, maximumValue);\n  }\n\n  private static void validateRequiredValue(String key, WebService.Param definition, @Nullable String value) {\n    boolean required = definition.isRequired();\n    if (required) {\n      checkArgument(value != null, format(MSG_PARAMETER_MISSING, key));\n    }\n  }\n\n  private static int validateAsNumeric(String key, String value) {\n    try {\n      return Integer.parseInt(value);\n    } catch (NumberFormatException exception) {\n      throw new IllegalArgumentException(format(\"'%s' value '%s' cannot be parsed as an integer\", key, value), exception);\n    }\n  }\n\n}\n","sourceCodeStart":226,"sourceCodeEnd":249,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java#L226-L249","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Validate the parameter client-side before sending, ensuring it matches ^-?\\d+$ and fits in an int.","Call valueAsLong(key) instead if the value may exceed Integer.MAX_VALUE.","Catch the IllegalArgumentException in the request handler and return a 400 response with a friendly message."],"exampleFix":"// before\nint page = request.valueAsInt(\"page\"); // throws for \"1.5\"\n\n// after\nString raw = request.value(\"page\");\nint page = raw != null && raw.matches(\"-?\\\\d+\") ? Integer.parseInt(raw) : 1;","handlingStrategy":"try-catch","validationCode":"// Java\nString raw = request.value(key);\nif (raw != null && !raw.matches(\"-?\\\\d+\")) {\n  throw new IllegalArgumentException(\"Not an integer: \" + key + \"=\" + raw);\n}","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n  int n = request.valueAsInt(key);\n} catch (IllegalArgumentException e) {\n  // return 400 with a user-friendly message\n}","preventionTips":["Validate numeric parameters client-side before sending.","Use valueAsLong when values may exceed Integer.MAX_VALUE.","Document parameter formats in the web service API."],"tags":["webservice","integer-parsing","request-validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}