{"record":{"id":"a16771345b43be3e","repo":"karatelabs/karate","slug":"set-prototype-method-called-with-non-object","errorCode":null,"errorMessage":"Set.prototype method called with non-object","messagePattern":"Set\\.prototype method called with non-object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java","lineNumber":160,"sourceCode":"     * callability. The result is the input to all seven set-methods.\n     */\n    private static final class SetRecord {\n        final ObjectLike setObj;\n        final long intSize;\n        final JsCallable has;\n        final JsCallable keys;\n\n        SetRecord(ObjectLike setObj, long intSize, JsCallable has, JsCallable keys) {\n            this.setObj = setObj;\n            this.intSize = intSize;\n            this.has = has;\n            this.keys = keys;\n        }\n    }\n\n    private static SetRecord getSetRecord(Object other, Context context) {\n        if (!(other instanceof ObjectLike obj)) {\n            throw JsErrorException.typeError(\"Set.prototype method called with non-object\");\n        }\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        Object rawSize = obj.getMember(\"size\", obj, cc);\n        Number numSize = Terms.toNumberCoerce(rawSize, cc);\n        double d = numSize.doubleValue();\n        if (Double.isNaN(d)) {\n            throw JsErrorException.typeError(\"Set-like object's size is not a number\");\n        }\n        // ToIntegerOrInfinity, then RangeError on negative — spec step 7.\n        long intSize;\n        if (Double.isInfinite(d)) {\n            intSize = d > 0 ? Long.MAX_VALUE : Long.MIN_VALUE;\n        } else {\n            intSize = (long) d;\n        }\n        if (intSize < 0) {\n            throw JsErrorException.rangeError(\"Set-like object's size is negative\");\n        }","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java#L142-L178","documentation":"Internal Set difference/intersection/symmetricDifference helpers require the `other` argument to be an object (a Set-like with size/has/keys). A primitive (number, string, null, undefined, boolean) was passed, so no SetRecord can be built.","triggerScenarios":"set.difference(42), set.intersection(null), set.symmetricDifference('abc') — any non-object `other` argument to a Set composition method.","commonSituations":"Passing an array literal or primitive where a Set was intended; a variable that was never constructed; calling the wrong method expecting array semantics.","solutions":["Pass an actual Set: set.difference(new Set(otherValues)).","Wrap plain objects with size/has/keys members to be Set-like before passing.","Guard with `other instanceof Set` (or typeof other === 'object') before calling."],"exampleFix":"// before\nmySet.difference(otherValues); // otherValues is an array\n// after\nmySet.difference(new Set(otherValues));","handlingStrategy":"validation","validationCode":"if (other === null || (typeof other !== 'object' && typeof other !== 'function')) throw new TypeError('other must be an object');","typeGuard":"function isSetLike(v) { return v !== null && typeof v === 'object'; }","tryCatchPattern":"try { return mySet.difference(other); } catch (e) { if (String(e).includes('non-object')) return mySet.difference(new Set(Array.from(other || []))); throw e; }","preventionTips":["Wrap iterables in new Set(...) before passing to difference/intersection.","Confirm variables hold objects, not primitives.","Prefer real Set instances for `other`."],"tags":["javascript","set","type-error"],"backgroundTag":"invalid-argument-value","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"}