karatelabs/karate · error · JsErrorException

Cannot set property ' ' which has only a getter

Error message

Cannot set property '${name}' which has only a getter

What it means

In Karate's JS engine, a property defined with only a getter (no setter) cannot be assigned. In strict mode the assignment throws this TypeError; in non-strict mode the write is silently ignored, matching ECMAScript semantics.

Solutions

  1. Define a setter for the property, or remove the assignment
  2. Write to a different, writable property or use a plain local variable
  3. Run in non-strict mode if the silent-ignore semantics are acceptable
  4. Update the backing object/Java bridge to expose a mutable field

Example fix

// before
obj.readOnlyProp = 42; // TypeError in strict mode
// after
let value = obj.readOnlyProp; // read instead, or define a setter
Defensive patterns

Strategy: type-guard

Validate before calling

// guard before writing in JS
function hasSetter(obj, prop) {
  const d = Object.getOwnPropertyDescriptor(obj, prop);
  return !!d && typeof d.set === 'function';
}
if (!hasSetter(obj, 'prop')) throw new Error('prop is read-only');

Type guard

function isWritable(obj, prop) {
  const d = Object.getOwnPropertyDescriptor(obj, prop);
  return d ? typeof d.set === 'function' || d.writable === true : true;
}

Try / catch

try {
  obj.readOnlyProp = value;
} catch (e) {
  if (e instanceof TypeError && String(e.message).includes('only a getter')) {
    // fall back to a writable local
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: `obj.prop = value` (via setByName -> AccessorSlot.write) where prop has a getter but no setter, with strict mode enabled.

Common situations: Assigning to read-only Java-bridged or engine-intrinsic properties; typos targeting a computed/read-only field; code written for sloppy mode run under strict mode in Karate JS.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/AccessorSlot.java:64

    @Override
    boolean isAccessor() {
        return true;
    }

    @Override
    Object read(Object receiver, CoreContext ctx) {
        if (getter == null || ctx == null) {
            return Terms.UNDEFINED;
        }
        return Interpreter.invokeGetter(getter, receiver, ctx);
    }

    @Override
    void write(Object receiver, Object newValue, CoreContext ctx, boolean strict) {
        if (setter == null) {
            if (strict) {
                throw JsErrorException.typeError(
                        "Cannot set property '" + name + "' which has only a getter");
            }
            return;
        }
        if (ctx != null) {
            Interpreter.invokeSetter(setter, receiver, newValue, ctx);
        }
    }

    @Override
    public String toString() {
        return name + "={get=" + getter + ", set=" + setter + "}";
    }

}

View on GitHub (pinned to a22eb90246)