{"record":{"id":"68cf094422fadfa4","repo":"karatelabs/karate","slug":"aggregateerror-requires-an-iterable-of-errors","errorCode":null,"errorMessage":"AggregateError requires an iterable of errors","messagePattern":"AggregateError requires an iterable of errors","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsErrorConstructor.java","lineNumber":97,"sourceCode":"     */\n    private static String toMessageString(Object value, CoreContext cc) {\n        if (value instanceof ObjectLike inner && cc != null) {\n            value = Terms.toPrimitive(inner, \"string\", cc);\n            if (cc.isError()) return null;\n        }\n        if (value == null) return \"null\";\n        if (value == Terms.UNDEFINED) return \"undefined\";\n        return value.toString();\n    }\n\n    /**\n     * AggregateError(errors, message?, options?): iterate errors into an Array\n     * own property, set message + cause as for plain Error. Spec §20.5.7.1.1.\n     */\n    private Object constructAggregate(Context context, Object[] args) {\n        Object errorsArg = args.length > 0 ? args[0] : Terms.UNDEFINED;\n        if (errorsArg == null || errorsArg == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"AggregateError requires an iterable of errors\");\n        }\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        List<Object> collected = new ArrayList<>();\n        JsIterator iter = IterUtils.getIterator(errorsArg, context);\n        while (iter.hasNext()) {\n            collected.add(iter.next());\n        }\n        String message = null;\n        if (args.length > 1 && args[1] != Terms.UNDEFINED) {\n            message = toMessageString(args[1], cc);\n            if (cc != null && cc.isError()) return Terms.UNDEFINED;\n        }\n        JsError instance = new JsError(errorPrototype);\n        if (message != null) {\n            instance.defineOwn(\"message\", message, MESSAGE_ATTRS);\n        }\n        instance.captureStack(cc);\n        installCauseFromOptions(instance, args, 2, cc);","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsErrorConstructor.java#L79-L115","documentation":"AggregateError's first argument must be iterable (spec §20.5.7.1.1 step 3); its elements become the `errors` property. Passing null or undefined means there is nothing to iterate, so Karate throws this TypeError before iteration begins.","triggerScenarios":"new AggregateError(), new AggregateError(null), new AggregateError(undefined), or a variable holding null passed as the errors argument (e.g. collected errors list that was never initialized).","commonSituations":"Promise.any-style aggregation code where the error list is conditionally built, refactoring that changed an empty array to null, unhandled Promise.any rejection handlers forwarding null.","solutions":["Pass an array (even empty): new AggregateError([], 'message').","Default the argument: new AggregateError(errors ?? [], msg).","Fix upstream code that produces null instead of an errors array."],"exampleFix":"// before\nthrow new AggregateError(errors, 'all failed'); // TypeError when errors is null\n// after\nthrow new AggregateError(errors ?? [], 'all failed');","handlingStrategy":"validation","validationCode":"if (!Array.isArray(errors)) throw new Error('AggregateError needs an array of errors');","typeGuard":"function isIterable(v) { return v != null && typeof v[Symbol.iterator] === 'function'; }","tryCatchPattern":"try { throw new AggregateError(errors ?? [], 'aggregated'); } catch (e) { if (e instanceof TypeError && /iterable of errors/.test(e.message)) { /* fix args */ } throw e; }","preventionTips":["Default the first constructor argument to [] with ?? or ||.","Initialize error-collector arrays before conditional loops fill them.","When wrapping Promise.any rejections, pass e.errors (always an array)."],"tags":["javascript","aggregate-error","iterable","type-error"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}