karatelabs/karate · error · RuntimeException

filter() needs two arguments: list and function

Error message

filter() needs two arguments: list and function

What it means

Karate's filter() JS utility builds a new list containing items for which a predicate function returns truthy. It requires two arguments: the source list and a callable predicate; fewer arguments throw this error.

Solutions

  1. Pass a JS function as the second argument: filter(list, function(x){ return x.active })
  2. Check that the first argument is actually a list
  3. If filtering a map by keys, use filterKeys() instead

Example fix

// before
var active = karate.filter(users);
// after
var active = karate.filter(users, u => u.active);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Array.isArray(list) || typeof fn !== 'function') throw new Error('filter requires a list and a function');

Type guard

function canFilter(args) { return args.length >= 2 && Array.isArray(args[0]) && typeof args[1] === 'function'; }

Try / catch

try { out = karate.filter(list, fn); } catch (e) { karate.log('filter failed: ' + e.message); }

Prevention

When it happens

Trigger: Calling filter(list) without the function, filter() with nothing, or passing a non-callable (string/JS object) as the second argument so the guard on arg count (or the cast) fails.

Common situations: Passing a native Array.prototype.filter-style callback string; forgetting the arrow function after refactoring; confusing filter with filterKeys which takes a map/keys instead of a function.

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

Appendix: source

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

                throw new RuntimeException("extractAll() needs three arguments: text, regex, group");
            }
            String text = args[0].toString();
            String regex = args[1].toString();
            int group = ((Number) args[2]).intValue();
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
            List<String> list = new ArrayList<>();
            while (matcher.find()) {
                list.add(matcher.group(group));
            }
            return list;
        };
    }

    static JavaInvokable filter() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("filter() needs two arguments: list and function");
            }
            List<?> list = (List<?>) args[0];
            JavaCallable fn = (JavaCallable) args[1];
            List<Object> result = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Object item = list.get(i);
                Object keep = fn.call(null, new Object[]{item, i});
                if (Boolean.TRUE.equals(keep)) {
                    result.add(item);
                }
            }
            return result;
        };
    }

    @SuppressWarnings("unchecked")
    static JavaInvokable filterKeys() {
        return args -> {

View on GitHub (pinned to a22eb90246)