redis/jedis · error · IllegalArgumentException
LIMIT must be non-negative
Error message
LIMIT must be non-negative
What it means
SUnionCardParams.limit() validates the cardinality cap client-side before the SUNIONCARD command is built. Jedis throws this IllegalArgumentException when a negative limit is passed, since Redis LIMIT only accepts non-negative values (0 means no cap).
Solutions
- Clamp or validate the limit before calling limit(), e.g. Math.max(0, limit).
- Omit the limit() call entirely when no cap is intended (LIMIT 0 is the default/no-limit).
- Fix the upstream computation that produces a negative value.
Example fix
// before SUnionCardParams params = new SUnionCardParams().limit(remaining - used); // after long limit = Math.max(0, remaining - used); SUnionCardParams params = new SUnionCardParams().limit(limit);
Defensive patterns
Strategy: validation
Validate before calling
if (limit < 0) throw new IllegalArgumentException("limit must be >= 0, got " + limit);
SUnionCardParams params = new SUnionCardParams().limit(limit); Type guard
boolean isValidLimit(long limit) { return limit >= 0; } Try / catch
try {
params = new SUnionCardParams().limit(limit);
} catch (IllegalArgumentException e) {
params = new SUnionCardParams(); // fall back to no limit
} Prevention
- Clamp computed limits with Math.max(0, n).
- Validate user-supplied pagination input at the boundary.
- Remember LIMIT 0 already means 'no cap' in Redis.
When it happens
Trigger: Calling new SUnionCardParams().limit(n) with any n < 0, e.g. limit(-1) or a limit computed from an unvalidated subtraction that underflows.
Common situations: Computing a limit as difference of counters (remaining - used) that went negative; passing user-supplied pagination input without clamping; integer arithmetic bugs producing negative caps.
Related errors
- failed to connect. Please check configuration and try again.
- IDMP-DURATION must be between 1 and 86400 seconds
- IDMP-MAXSIZE must be between 1 and 10000
- client info cannot contain spaces, newlines or special…
- DriverInfo must not be null
AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08).
Data as JSON: /api/errors/15e52a75c0616456.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/redis/clients/jedis/params/SUnionCardParams.java:46
}
/**
* Return an approximate cardinality using HyperLogLog (standard error ~0.81%).
* @return SUnionCardParams
*/
public SUnionCardParams approx() {
this.approx = true;
return this;
}
/**
* Cap the returned cardinality at {@code limit}. {@code LIMIT 0} means no limit.
* @param limit non-negative cap
* @return SUnionCardParams
*/
public SUnionCardParams limit(long limit) {
if (limit < 0) {
throw new IllegalArgumentException("LIMIT must be non-negative");
}
this.limit = limit;
return this;
}
@Override
public void addParams(CommandArguments args) {
if (approx) {
args.add(Keyword.APPROX);
}
if (limit != null) {
args.add(Keyword.LIMIT);
args.add(limit);
}
}
@Override
public boolean equals(Object o) {View on GitHub (pinned to 6dac31d4c2)