{"record":{"id":"889c2dfa863d5ed9","repo":"karatelabs/karate","slug":"cannot-set-property-name-which-has-only-a-getter","errorCode":null,"errorMessage":"Cannot set property '${name}' which has only a getter","messagePattern":"Cannot set property '(.+?)' which has only a getter","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/AccessorSlot.java","lineNumber":64,"sourceCode":"\n    @Override\n    boolean isAccessor() {\n        return true;\n    }\n\n    @Override\n    Object read(Object receiver, CoreContext ctx) {\n        if (getter == null || ctx == null) {\n            return Terms.UNDEFINED;\n        }\n        return Interpreter.invokeGetter(getter, receiver, ctx);\n    }\n\n    @Override\n    void write(Object receiver, Object newValue, CoreContext ctx, boolean strict) {\n        if (setter == null) {\n            if (strict) {\n                throw JsErrorException.typeError(\n                        \"Cannot set property '\" + name + \"' which has only a getter\");\n            }\n            return;\n        }\n        if (ctx != null) {\n            Interpreter.invokeSetter(setter, receiver, newValue, ctx);\n        }\n    }\n\n    @Override\n    public String toString() {\n        return name + \"={get=\" + getter + \", set=\" + setter + \"}\";\n    }\n\n}\n","sourceCodeStart":46,"sourceCodeEnd":80,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/AccessorSlot.java#L46-L80","documentation":"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.","triggerScenarios":"`obj.prop = value` (via setByName -> AccessorSlot.write) where prop has a getter but no setter, with strict mode enabled.","commonSituations":"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.","solutions":["Define a setter for the property, or remove the assignment","Write to a different, writable property or use a plain local variable","Run in non-strict mode if the silent-ignore semantics are acceptable","Update the backing object/Java bridge to expose a mutable field"],"exampleFix":"// before\nobj.readOnlyProp = 42; // TypeError in strict mode\n// after\nlet value = obj.readOnlyProp; // read instead, or define a setter\n","handlingStrategy":"type-guard","validationCode":"// guard before writing in JS\nfunction hasSetter(obj, prop) {\n  const d = Object.getOwnPropertyDescriptor(obj, prop);\n  return !!d && typeof d.set === 'function';\n}\nif (!hasSetter(obj, 'prop')) throw new Error('prop is read-only');","typeGuard":"function isWritable(obj, prop) {\n  const d = Object.getOwnPropertyDescriptor(obj, prop);\n  return d ? typeof d.set === 'function' || d.writable === true : true;\n}","tryCatchPattern":"try {\n  obj.readOnlyProp = value;\n} catch (e) {\n  if (e instanceof TypeError && String(e.message).includes('only a getter')) {\n    // fall back to a writable local\n    return;\n  }\n  throw e;\n}","preventionTips":["Use Object.getOwnPropertyDescriptor to check writability before assigning","Do not assign to engine-intrinsic or Java-bridged read-only properties","Keep strict mode on so silent failures become visible TypeErrors"],"tags":["javascript","typeerror","getter-only","strict-mode"],"backgroundTag":"type-mismatch","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"}