karatelabs/karate · error · IllegalArgumentException

missing argument

Error message

missing argument {index}

What it means

Args.extract converts a JS call's positional arguments into typed Java parameters; when the argument at the given index is absent and not marked optional, it throws IllegalArgumentException 'missing argument {index}'.

Solutions

  1. Count and supply all required positional arguments at the call site
  2. Check the function's signature/docs for which parameters are optional and which have defaults
  3. If the caller builds an args array dynamically, assert its length before invoking
  4. For optional parameters, pass explicit placeholders (null/undefined) at earlier positions rather than shortening the array

Example fix

// before (JS)
karate.call('delete-user');            // required id arg missing
// after (JS)
karate.call('delete-user', userId);    // supply every required argument
Defensive patterns

Strategy: validation

Validate before calling

// JS side, before invoking
if (arguments.length < 2) {
    throw new Error('expected 2 arguments, got ' + arguments.length);
}
apiCall(arguments[0], arguments[1]);

Try / catch

// Java side
try {
    return invoke(args);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("missing argument")) {
        throw new ScriptCallException("call site supplied too few arguments: " + e.getMessage());
    }
    throw e;
}

Prevention

When it happens

Trigger: Invoking a registered JS function/method with fewer positional arguments than required — e.g. calling a Karate JS API with omitted required parameters, or spreading an array shorter than the parameter list.

Common situations: Calling Karate JS built-ins from a script with arguments accidentally dropped during refactoring; conditionally-built argument arrays that end up too short; documentation examples used with older/newer signatures.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/33988f89e9a6aecb. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/Args.java:95

        final Class<T> type;
        final T defaultValue;
        final boolean optional;

        Arg(Class<T> type, T defaultValue, boolean optional) {
            this.type = type;
            this.defaultValue = defaultValue;
            this.optional = optional;
        }

        public Arg<T> optional(T defaultValue) {
            return new Arg<>(type, defaultValue, true);
        }

        @SuppressWarnings("unchecked")
        T extract(Object[] args, int index) {
            if (index >= args.length) {
                if (optional) return defaultValue;
                throw new IllegalArgumentException("missing argument " + index);
            }
            Object raw = args[index];
            if (type == String.class) return (T) raw.toString();
            if (type == Integer.class) return (T) Integer.valueOf(((Number) raw).intValue());
            if (type == Double.class) return (T) Double.valueOf(((Number) raw).doubleValue());
            return type.cast(raw);
        }
    }

}

View on GitHub (pinned to a22eb90246)