grpc/grpc-java · error · IllegalArgumentException

value ' ' for key ' ' is not a long integer

Error message

value '%s' for key '%s' is not a long integer

What it means

getNumberAsLong accepts string-encoded longs, parsed with Long.parseLong. A String that cannot be parsed (wrong characters, decimal point, overflow) produces IllegalArgumentException 'value ... is not a long integer'. This keeps long config fields strictly validated.

Solutions

  1. Fix the string to a valid signed 64-bit decimal integer (strip commas/units, remove decimal point).
  2. If the number exceeds Long range, restructure the config (e.g. split high/low words or use a smaller unit).
  3. Pre-validate with Long.parseLong in your own loader to raise a clearer error.

Example fix

// before
{"flowControlWindow": "1,048,576"}
// after
{"flowControlWindow": 1048576}
Defensive patterns

Strategy: validation

Validate before calling

Object v = config.get("flowControlWindow");
if (v instanceof String) {
  try { Long.parseLong((String) v); }
  catch (NumberFormatException e) { throw new IllegalArgumentException("flowControlWindow is not a long"); }
}

Type guard

boolean isLongString(Object v) {
  if (!(v instanceof String)) return false;
  try { Long.parseLong((String) v); return true; } catch (NumberFormatException e) { return false; }
}

Try / catch

try {
  long l = JsonUtil.getNumberAsLong(config, "flowControlWindow");
} catch (IllegalArgumentException e) {
  log.error("Invalid long value in config: " + e.getMessage());
}

Prevention

When it happens

Trigger: JsonUtil.getNumberAsLong(map, key) with a String value like "abc", "1.5", "99999999999999999999" (overflow past Long.MAX_VALUE), or a signed/whitespace-polluted string.

Common situations: Quoted numbers in service config JSON; 64-bit values pasted from other systems with formatting (commas "1,000,000"); values exceeding Long.MAX_VALUE such as unsigned 64-bit IDs.

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 grpc/grpc-java@64daddc1f3 (2026-09-08). Data as JSON: /api/errors/d3d5299abf9c25e2. Report an issue: GitHub.

Appendix: source

Thrown at core/src/main/java/io/grpc/internal/JsonUtil.java:178

  public static Long getNumberAsLong(Map<String, ?> obj, String key) {
    assert key != null;
    if (!obj.containsKey(key)) {
      return null;
    }
    Object value = obj.get(key);
    if (value instanceof Double) {
      Double d = (Double) value;
      long l = d.longValue();
      if (l != d) {
        throw new ClassCastException("Number expected to be long: " + d);
      }
      return l;
    }
    if (value instanceof String) {
      try {
        return Long.parseLong((String) value);
      } catch (NumberFormatException e) {
        throw new IllegalArgumentException(
            String.format("value '%s' for key '%s' is not a long integer", value, key));
      }
    }
    throw new IllegalArgumentException(
        String.format("value '%s' for key '%s' is not a long integer", value, key));
  }

  /**
   * Gets a string from an object for the given key.  If the key is not present, this returns null.
   * If the value is not a String, throws an exception.
   */
  @Nullable
  public static String getString(Map<String, ?> obj, String key) {
    assert key != null;
    if (!obj.containsKey(key)) {
      return null;
    }
    Object value = obj.get(key);

View on GitHub (pinned to 64daddc1f3)