karatelabs/karate · error · JsErrorException
Iterator value is not an entry object
Error message
Iterator value {entry} is not an entry object What it means
When `new Map(iterable)` consumes an iterable, each yielded value must be an entry object (a List/array of [key, value] or an object with 0/1 properties). Karate throws this TypeError when an iterator yields something else — a scalar, a function, etc. — so no key/value pair can be extracted.
Solutions
- Wrap each element as a [key, value] pair: `new Map([[1,'a'],[2,'b']])`
- Use `Object.entries(obj)` instead of `Object.keys(obj)` when building a Map from an object
- Log or inspect the iterable before passing to new Map() to confirm each element is a 2-element array or entry object
- Filter/map the iterable first if the source data has heterogeneous shapes
Example fix
// before const m = new Map([1, 2, 3]); // TypeError // after const m = new Map([[1, null], [2, null], [3, null]]); // or [1,2,3].map(k => [k, k*2])
Defensive patterns
Strategy: validation
Validate before calling
// every element must be a pair
if (!arr.every(e => Array.isArray(e) && e.length >= 1)) {
throw new Error('iterable for new Map() must yield [key, value] entries');
} Type guard
function isEntry(e) { return Array.isArray(e) || (e !== null && typeof e === 'object'); } Try / catch
try {
const m = new Map(iterable);
} catch (e) {
if (String(e.message).includes('is not an entry object')) {
throw new Error('bad input to new Map: ' + e.message);
} else throw e;
} Prevention
- Use Object.entries(obj), not Object.keys(obj), to build Maps from objects
- Inspect the iterable's first element before constructing
- Keep [key, value] pair shapes intact through array transforms
When it happens
Trigger: `new Map([1, 2, 3])` — iterator yields numbers, not pairs; `new Map('ab')` — each char is a string; `new Map(gen())` where the generator yields non-entry values; a nested array flattened by mistake so inner items are scalars.
Common situations: Hand-writing arrays of keys instead of [key, value] pairs; converting an Object to Map via Object.keys (yields strings) instead of Object.entries; porting JS from environments where 2-element arrays were accidentally flattened.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- NoSuchElementException
- Result of the Symbol.iterator method is not an object
- iterator result is not an object
- The iterator does not provide a 'throw' method
- iterator.throw is not a function
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/505f876bd65cdec0.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsMapConstructor.java:81
}
JsMap map = new JsMap();
if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) {
return map;
}
// Look up `set` on the freshly-constructed map's prototype chain — honors
// user-overridden Map.prototype.set per spec 24.1.1.1 step 9.
Object setFn = map.getMember("set");
if (!(setFn instanceof JsCallable adder)) {
throw JsErrorException.typeError("Map.prototype.set is not callable");
}
JsIterator iter = IterUtils.getIterator(args[0], context);
CoreContext cc = context instanceof CoreContext c ? c : null;
Object savedThis = cc != null ? cc.thisObject : null;
try {
while (iter.hasNext()) {
Object entry = iter.next();
if (!(entry instanceof List) && !(entry instanceof ObjectLike)) {
throw JsErrorException.typeError("Iterator value " + entry + " is not an entry object");
}
Object k;
Object v;
if (entry instanceof List<?> list) {
k = list.isEmpty() ? Terms.UNDEFINED : list.get(0);
v = list.size() < 2 ? Terms.UNDEFINED : list.get(1);
} else {
ObjectLike ol = (ObjectLike) entry;
k = ol.getMember("0");
v = ol.getMember("1");
if (k == null) k = Terms.UNDEFINED;
if (v == null) v = Terms.UNDEFINED;
}
if (cc != null) cc.thisObject = map;
adder.call(context, new Object[]{k, v});
if (cc != null && cc.isError()) {
return map;
}View on GitHub (pinned to a22eb90246)