{"record":{"id":"78bb148329194e50","repo":"karatelabs/karate","slug":"object-defineproperties-called-on-non-object","errorCode":null,"errorMessage":"Object.defineProperties called on non-object","messagePattern":"Object\\.defineProperties called on non-object","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java","lineNumber":881,"sourceCode":"            // accessor support so {@code Object.defineProperty(Foo.prototype,\n            // \"x\", {get: …})} works — instances inheriting from the prototype\n            // resolve the accessor via the chain walk in PropertyAccess.\n            p.defineOwnAccessor(key, getter, setter, attrs);\n        }\n        // Other ObjectLikes (Map, raw List, etc.) don't model accessor\n        // descriptors. No reachable test path exercises that today.\n    }\n\n    private static byte ownAttrs(Object obj, String key) {\n        if (obj instanceof JsObject jo) return jo.getOwnAttrs(key);\n        if (obj instanceof JsArray ja) return ja.getOwnAttrs(key);\n        if (obj instanceof Prototype p) return p.getOwnAttrs(key);\n        return JsObject.ATTRS_DEFAULT;\n    }\n\n    private Object defineProperties(Context context, Object[] args) {\n        if (args.length < 1 || !(args[0] instanceof ObjectLike || args[0] instanceof Map)) {\n            throw JsErrorException.typeError(\"Object.defineProperties called on non-object\");\n        }\n        Object source = args.length < 2 ? null : args[1];\n        if (source == null || source == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"Cannot convert undefined or null to object\");\n        }\n        // Spec ToObject(primitive) yields a wrapper. Boolean / number / empty\n        // string wrappers have no enumerable own keys, so the loop iterates\n        // nothing and returns the target — {@code ownKeys} below returns an\n        // empty set for those. A non-empty string wrapper exposes indexed\n        // characters as own properties; reading the first character and\n        // running ToPropertyDescriptor on it lands on TypeError (\"Property\n        // descriptor must be an object\"). Short-circuit that here rather\n        // than wiring full wrapper iteration.\n        if (source instanceof String s && !s.isEmpty()) {\n            throw JsErrorException.typeError(\"Property descriptor must be an object\");\n        }\n        // Spec §20.1.2.3 walks enumerable own keys via [[OwnPropertyKeys]] +\n        // [[GetOwnProperty]], reading each descriptor via [[Get]] so accessor","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java#L863-L899","documentation":"Object.defineProperties(target, props) requires the target to be an object (ObjectLike or Map). Passing a primitive (number, string, boolean, null, undefined) as the first argument throws this TypeError instead of implicitly wrapping.","triggerScenarios":"Object.defineProperties(42, {...}), Object.defineProperties('str', {...}), Object.defineProperties(null, {...}) — any non-object first argument.","commonSituations":"Variable that was expected to be an object but is a primitive; API returning a string/number where an object was assumed; forgetting to parse JSON before calling.","solutions":["Pass an actual object: Object.defineProperties({}, props)","Coerce explicitly with Object(target) if you want wrapper semantics","Validate the target before the call: if (target && typeof target === 'object')","Fix the upstream source so the variable actually holds an object"],"exampleFix":"// before\nObject.defineProperties(someValue, { x: { value: 1 } }); // throws if primitive\n// after\nconst target = (someValue && typeof someValue === 'object') ? someValue : {};\nObject.defineProperties(target, { x: { value: 1 } });","handlingStrategy":"validation","validationCode":"if (target == null || (typeof target !== 'object' && typeof target !== 'function')) throw new TypeError('defineProperties target must be an object');","typeGuard":"function isDefinePropertiesTarget(t) { return t != null && (typeof t === 'object' || typeof t === 'function'); }","tryCatchPattern":"try { Object.defineProperties(target, props); } catch (e) { if (String(e.message).includes('non-object')) target = {}; /* retry */ else throw e; }","preventionTips":["Assert object-ness of targets at API boundaries","Fix upstream producers that return primitives where objects are expected","Parse JSON payloads before passing them as targets"],"tags":["javascript","defineproperties","typeerror","argument"],"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-19T12:17:13.211Z"}