quarkusio/quarkus · error · IllegalArgumentException

Cannot set XX and NX together

Error message

Cannot set XX and NX together

What it means

JsonSetArgs builds extra arguments for the JSON.SET command. Redis forbids the XX (only update if exists) and NX (only set if absent) flags together since they are mutually exclusive; the client validates this eagerly in toArgs() and throws IllegalArgumentException before the command is sent. Setting both is always a logic error because the two conditions can never both hold.

Source

Thrown at extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/json/JsonSetArgs.java:36

    public JsonSetArgs nx() {
        this.nx = true;
        return this;
    }

    /**
     * Only update elements that already exist. Never add elements.
     *
     * @return the current {@code GeoaddArgs}
     **/
    public JsonSetArgs xx() {
        this.xx = true;
        return this;
    }

    @Override
    public List<Object> toArgs() {
        if (xx && nx) {
            throw new IllegalArgumentException("Cannot set XX and NX together");
        }
        List<Object> args = new ArrayList<>();
        if (xx) {
            args.add("XX");
        }
        if (nx) {
            args.add("NX");
        }
        return args;
    }
}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Call only one of xx() or nx() — decide whether the set should be insert-only (nx) or update-only (xx)
  2. Validate exclusivity in your own code before building the args if flags come from configuration
  3. If both semantics are somehow requested, drop the conflicting flag based on your actual intent

Example fix

// before
JsonSetArgs args = new JsonSetArgs().xx().nx(); // throws
// after
JsonSetArgs args = onlyUpdate
    ? new JsonSetArgs().xx()
    : new JsonSetArgs().nx();
Defensive patterns

Strategy: validation

Validate before calling

JsonSetArgs args = new JsonSetArgs();
if (wantInsertOnly && wantUpdateOnly) {
    throw new IllegalArgumentException("JSON.SET options XX and NX are mutually exclusive");
}
if (wantInsertOnly) args.nx();
else if (wantUpdateOnly) args.xx();

Try / catch

try {
    jsonCommands.jsonSet(key, value, args);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("XX and NX")) {
        // fix builder usage: only one flag allowed
    }
}

Prevention

When it happens

Trigger: Building JsonSetArgs with a chained/fluent call to both xx() and nx(), e.g. new JsonSetArgs().xx().nx() — often from copy-paste or programmatic assembly where flags are OR-accumulated from booleans/config.

Common situations: Fluent-builder misuse where an extra .nx() is left from a previous variant; mapping user options onto args without validating exclusivity; helper methods that add both flags conditionally.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/05cc6ce1687cb662. Report an issue: GitHub.