{"record":{"id":"181878a6b786e41a","repo":"karatelabs/karate","slug":"iterator-return-result-is-not-an-object","errorCode":null,"errorMessage":"iterator 'return' result is not an object","messagePattern":"iterator 'return' result is not an object","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/IterUtils.java","lineNumber":475,"sourceCode":"                    }\n                    Object result;\n                    if (ctx instanceof CoreContext cc) {\n                        Object savedThis = cc.thisObject;\n                        cc.thisObject = iterObj;\n                        try {\n                            result = retCallable.call(cc, EMPTY_ARGS);\n                        } finally {\n                            cc.thisObject = savedThis;\n                        }\n                    } else {\n                        result = retCallable.call(ctx, EMPTY_ARGS);\n                    }\n                    if (isJsErrored(ctx)) {\n                        if (dueToError) clearError(ctx); // return() threw, discard\n                        return;                           // else propagate return()'s throw\n                    }\n                    if (!dueToError && !(result instanceof ObjectLike)) {\n                        throw JsErrorException.typeError(\"iterator 'return' result is not an object\");\n                    }\n                } finally {\n                    if (hadError) {\n                        ((CoreContext) ctx).stopAndThrow(savedError); // original completion wins\n                    }\n                }\n            }\n        };\n    }\n\n    private static void clearError(Context ctx) {\n        if (ctx instanceof CoreContext cc && cc.isError()) {\n            cc.reset();\n        }\n    }\n\n    private static final Object[] EMPTY_ARGS = new Object[0];\n","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/IterUtils.java#L457-L493","documentation":"When IteratorClose runs outside an error path (dueToError == false), the spec requires the result of calling the iterator's `return()` to be an object. If return() returns a primitive, null, or undefined, karate throws this TypeError. If an error was already in flight, return()'s bad result is tolerated and the original completion/error takes precedence.","triggerScenarios":"Breaking out of a for-of loop whose iterator's `return()` method returns nothing (implicit undefined) or a primitive, e.g. `return: function() { close(); }` with no return statement.","commonSituations":"Custom iterators with cleanup hooks that forget to return `{done:true}` from return(); resources (files, connections) wrapped as iterators whose return() just calls a void cleanup function.","solutions":["Return an object from return(): `return: function() { cleanup(); return { done: true }; }`.","Omit `return` entirely if there is nothing meaningful to do — a missing return method is legal.","Wrap void cleanups: `return: function() { cleanup(); return {}; }`."],"exampleFix":"// before\nreturn: function() { file.close(); }\n// after\nreturn: function() { file.close(); return { done: true }; }","handlingStrategy":"validation","validationCode":"var r = it.return();\nif (r == null || typeof r !== 'object') throw new TypeError(\"return() must return an object\");","typeGuard":"function returnsObject(fn) { var r = fn(); return r != null && typeof r === 'object'; }","tryCatchPattern":"try { for (var x of it) { if (stop) break; } } catch (e) { if (String(e).indexOf(\"'return' result is not an object\") !== -1) { /* resource already cleaned up; log */ } else { throw e; } }","preventionTips":["return() must return {done:true} (or at least an object), never void","If cleanup is side-effect only, either return {} or omit return entirely","Test early-break paths of every custom iterator"],"tags":["javascript","iterator-protocol","cleanup","typeerror"],"backgroundTag":"unexpected-response-shape","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"}