karatelabs/karate · error · TypeError

Set.prototype method called with non-object

Error message

Set.prototype method called with non-object

What it means

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.

Solutions

  1. Pass an actual Set: set.difference(new Set(otherValues)).
  2. Wrap plain objects with size/has/keys members to be Set-like before passing.
  3. Guard with `other instanceof Set` (or typeof other === 'object') before calling.

Example fix

// before
mySet.difference(otherValues); // otherValues is an array
// after
mySet.difference(new Set(otherValues));
Defensive patterns

Strategy: validation

Validate before calling

if (other === null || (typeof other !== 'object' && typeof other !== 'function')) throw new TypeError('other must be an object');

Type guard

function isSetLike(v) { return v !== null && typeof v === 'object'; }

Try / catch

try { return mySet.difference(other); } catch (e) { if (String(e).includes('non-object')) return mySet.difference(new Set(Array.from(other || []))); throw e; }

Prevention

When it happens

Trigger: set.difference(42), set.intersection(null), set.symmetricDifference('abc') — any non-object `other` argument to a Set composition method.

Common situations: Passing an array literal or primitive where a Set was intended; a variable that was never constructed; calling the wrong method expecting array semantics.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/a16771345b43be3e. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java:160

     * callability. The result is the input to all seven set-methods.
     */
    private static final class SetRecord {
        final ObjectLike setObj;
        final long intSize;
        final JsCallable has;
        final JsCallable keys;

        SetRecord(ObjectLike setObj, long intSize, JsCallable has, JsCallable keys) {
            this.setObj = setObj;
            this.intSize = intSize;
            this.has = has;
            this.keys = keys;
        }
    }

    private static SetRecord getSetRecord(Object other, Context context) {
        if (!(other instanceof ObjectLike obj)) {
            throw JsErrorException.typeError("Set.prototype method called with non-object");
        }
        CoreContext cc = context instanceof CoreContext c ? c : null;
        Object rawSize = obj.getMember("size", obj, cc);
        Number numSize = Terms.toNumberCoerce(rawSize, cc);
        double d = numSize.doubleValue();
        if (Double.isNaN(d)) {
            throw JsErrorException.typeError("Set-like object's size is not a number");
        }
        // ToIntegerOrInfinity, then RangeError on negative — spec step 7.
        long intSize;
        if (Double.isInfinite(d)) {
            intSize = d > 0 ? Long.MAX_VALUE : Long.MIN_VALUE;
        } else {
            intSize = (long) d;
        }
        if (intSize < 0) {
            throw JsErrorException.rangeError("Set-like object's size is negative");
        }

View on GitHub (pinned to a22eb90246)