karatelabs/karate · error · RuntimeException

append() needs at least two arguments

Error message

append() needs at least two arguments

What it means

Karate's append() JS utility returns a new list formed by appending items to the first argument. It requires at least two arguments: the base list (or value) plus at least one item. With fewer than two arguments it throws this error rather than returning a malformed result.

Solutions

  1. Pass the base list as the first argument and at least one item as the second
  2. If you meant to mutate an existing list, use appendTo(list, item) instead
  3. Check dynamically-built argument arrays for length before invoking

Example fix

// before
var result = karate.append(list);
// after
var result = karate.append(list, 'newItem');
Defensive patterns

Strategy: validation

Validate before calling

if (arguments.length < 2) throw new Error('append() needs a list and at least one item');

Type guard

function canAppend(args) { return Array.isArray(args) && args.length >= 2; }

Try / catch

try { result = karate.append(list, item); } catch (e) { karate.log('append needs list + item: ' + e.message); }

Prevention

When it happens

Trigger: Calling append() with no arguments, or with only one argument such as append(list) or append(item), inside a karate JS block.

Common situations: Confusing append() with appendTo() (in-place variant); forgetting the item while dynamically building the call; refactoring that removed the second argument; passing only a spread of one element.

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

Appendix: source

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

 * @see KarateJsContext for runtime context interface
 */
public class KarateJsUtils {

    private static final Logger logger = LogContext.RUNTIME_LOGGER;

    private static final Configuration jsonPathConfig = Configuration.defaultConfiguration()
            .addOptions(Option.SUPPRESS_EXCEPTIONS);

    private KarateJsUtils() {
        // utility class
    }

    // ========== Collection Utilities ==========

    static JavaInvokable append() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("append() needs at least two arguments");
            }
            List<Object> result = new ArrayList<>();
            Object first = args[0];
            if (first instanceof List) {
                result.addAll((List<?>) first);
            } else {
                result.add(first);
            }
            for (int i = 1; i < args.length; i++) {
                Object item = args[i];
                if (item instanceof List) {
                    result.addAll((List<?>) item);
                } else {
                    result.add(item);
                }
            }
            return result;
        };

View on GitHub (pinned to a22eb90246)