karatelabs/karate · error · RuntimeException

repeat() needs two arguments: count and function

Error message

repeat() needs two arguments: count and function

What it means

repeat() is a Karate JS utility that calls a function N times (the function receives the index) and collects results in a List. It throws this error when called with fewer than two arguments, because both the count and the function are mandatory.

Solutions

  1. Supply both arguments: repeat(count, fn) where fn(i) returns the value for each iteration.
  2. Ensure the first argument is a number and the second an actual function.
  3. Verify the count is defined and non-null before the call.

Example fix

// before
def list = repeat(3)
// after
def list = repeat(3, function(i){ return 'item' + i })
Defensive patterns

Strategy: validation

Validate before calling

def list = (count && fn) ? repeat(count, fn) : []

Prevention

When it happens

Trigger: karate.repeat() with zero or one argument, e.g. repeat(5) without the function, or repeat() entirely; a null count variable reducing the bound arguments.

Common situations: Forgetting the callback when translating a for-loop; passing the arguments in the wrong order; the function variable is undefined so the arity check fails.

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/25df9f2a7e3f14c2. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:452

        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("prettyXml() needs one argument");
            }
            Object obj = args[0];
            if (obj instanceof Node) {
                return Xml.toString((Node) obj, true);
            } else if (obj instanceof String) {
                return Xml.toString(Xml.toXmlDoc((String) obj), true);
            } else {
                throw new RuntimeException("prettyXml() argument must be XML node or string");
            }
        };
    }

    static JavaInvokable repeat() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("repeat() needs two arguments: count and function");
            }
            int count = ((Number) args[0]).intValue();
            JavaCallable fn = (JavaCallable) args[1];
            List<Object> result = new ArrayList<>();
            for (int i = 0; i < count; i++) {
                result.add(fn.call(null, new Object[]{i}));
            }
            return result;
        };
    }

    static JavaInvokable sizeOf() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("sizeOf() needs one argument");
            }
            Object obj = args[0];
            if (obj instanceof List) {

View on GitHub (pinned to a22eb90246)