{"record":{"id":"90cf3106a10fb7aa","repo":"karatelabs/karate","slug":"nosuchelementexception-jsglobalthis","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"java.util.NoSuchElementException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsGlobalThis.java","lineNumber":243,"sourceCode":"                    if (bindings().isTombstoned(e.getKey())) continue;\n                    if (isEnumerable(e.getKey())) {\n                        peeked = e;\n                        return true;\n                    }\n                }\n                peeked = null;\n                return false;\n            }\n\n            @Override\n            public boolean hasNext() {\n                return peeked != null || advance();\n            }\n\n            @Override\n            public KeyValue next() {\n                if (peeked == null && !advance()) {\n                    throw new NoSuchElementException();\n                }\n                Map.Entry<String, Object> entry = peeked;\n                peeked = null;\n                return new KeyValue(JsGlobalThis.this, index++, entry.getKey(), entry.getValue());\n            }\n        };\n    }\n\n    @Override\n    public Map<String, Object> toMap() {\n        // Raw view of the unified store — both visible and hidden entries,\n        // identity preserved (no JsFunction → JsFunctionWrapper). Built-ins\n        // not yet lazy-cached aren't here; Object.keys(globalThis) only sees\n        // realized entries (and gets filtered down further by enumerability\n        // via getOwnAttrs above). Merge in any JsObject.props entries — those\n        // hold accessor descriptors installed via Object.defineProperty(this, …)\n        // (BindingSlot is data-only).\n        Map<String, Object> raw = bindings().getRawMap();","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsGlobalThis.java#L225-L261","documentation":"JsGlobalThis's property iterator (used to enumerate the global object's bindings) throws NoSuchElementException when next() is called after the last entry. Like the other JS iterators, callers must consult hasNext() first; the exception signals iteration past the end of the global-object entry set.","triggerScenarios":"Advancing the globalThis entry iterator past its final key; internal enumeration loops (for...in / Object.keys style) driven by buggy or duplicated consumption of the same iterator.","commonSituations":"Host Java code enumerating global variables; engine bugs when globals are added/removed mid-enumeration; iterating after a prior full pass without resetting.","solutions":["Always pair next() with hasNext() (or a for-each loop).","Get a fresh iterator for each enumeration pass.","Avoid mutating the global object while enumerating it; collect keys first, then mutate.","If thrown from library internals during global enumeration, report with a repro; upgrade karate-js."],"exampleFix":"// before\nKeyValue kv = it.next(); // assumes another entry exists\n// after\nif (it.hasNext()) { KeyValue kv = it.next(); }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"boolean canAdvance(Iterator<?> it) { return it != null && it.hasNext(); }","tryCatchPattern":"try { KeyValue kv = it.next(); } catch (NoSuchElementException e) { /* exhausted: treat as end of enumeration */ }","preventionTips":["Use hasNext()-guarded loops for global-object enumeration.","Collect global keys into a list first, then iterate the snapshot.","Do not define/delete globals during enumeration.","Allocate a fresh iterator per enumeration pass."],"tags":["javascript","iterator","globalthis"],"backgroundTag":"index-out-of-bounds","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"}