{"record":{"id":"6817d4ea5b51d842","repo":"karatelabs/karate","slug":"append-needs-at-least-two-arguments","errorCode":null,"errorMessage":"append() needs at least two arguments","messagePattern":"append\\(\\) needs at least two arguments","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":88,"sourceCode":" * @see KarateJsContext for runtime context interface\n */\npublic class KarateJsUtils {\n\n    private static final Logger logger = LogContext.RUNTIME_LOGGER;\n\n    private static final Configuration jsonPathConfig = Configuration.defaultConfiguration()\n            .addOptions(Option.SUPPRESS_EXCEPTIONS);\n\n    private KarateJsUtils() {\n        // utility class\n    }\n\n    // ========== Collection Utilities ==========\n\n    static JavaInvokable append() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"append() needs at least two arguments\");\n            }\n            List<Object> result = new ArrayList<>();\n            Object first = args[0];\n            if (first instanceof List) {\n                result.addAll((List<?>) first);\n            } else {\n                result.add(first);\n            }\n            for (int i = 1; i < args.length; i++) {\n                Object item = args[i];\n                if (item instanceof List) {\n                    result.addAll((List<?>) item);\n                } else {\n                    result.add(item);\n                }\n            }\n            return result;\n        };","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L70-L106","documentation":"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.","triggerScenarios":"Calling append() with no arguments, or with only one argument such as append(list) or append(item), inside a karate JS block.","commonSituations":"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.","solutions":["Pass the base list as the first argument and at least one item as the second","If you meant to mutate an existing list, use appendTo(list, item) instead","Check dynamically-built argument arrays for length before invoking"],"exampleFix":"// before\nvar result = karate.append(list);\n// after\nvar result = karate.append(list, 'newItem');","handlingStrategy":"validation","validationCode":"if (arguments.length < 2) throw new Error('append() needs a list and at least one item');","typeGuard":"function canAppend(args) { return Array.isArray(args) && args.length >= 2; }","tryCatchPattern":"try { result = karate.append(list, item); } catch (e) { karate.log('append needs list + item: ' + e.message); }","preventionTips":["Remember append() returns a new list; use appendTo() for in-place mutation","Count arguments when building calls dynamically","Keep a list of karate.* util signatures handy for the team"],"tags":["argument-count","javascript","karate","list"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}