{"record":{"id":"05ff8b0174f274ad","repo":"karatelabs/karate","slug":"property-descriptor-must-be-an-object","errorCode":null,"errorMessage":"Property descriptor must be an object","messagePattern":"Property descriptor must be an object","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java","lineNumber":556,"sourceCode":"    /** Returns the own {@link AccessorSlot} at {@code key} when one exists,\n     *  {@code null} otherwise (own data property, missing, or unsupported\n     *  type). Scoped to own properties only — for the chain-walking variant\n     *  see {@code PropertyAccess#findAccessorInChain}. */\n    private static AccessorSlot ownAccessorSlot(Object obj, String key) {\n        PropertySlot s = PropertyAccess.ownSlot(obj, key);\n        return s instanceof AccessorSlot acc ? acc : null;\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    Object defineProperty(Context context, Object[] args) {\n        if (args.length < 1 || !(args[0] instanceof ObjectLike || args[0] instanceof Map)) {\n            throw JsErrorException.typeError(\"Object.defineProperty called on non-object\");\n        }\n        if (args.length < 2) {\n            throw JsErrorException.typeError(\"property key is null\");\n        }\n        if (args.length < 3 || args[2] == null || args[2] == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"Property descriptor must be an object\");\n        }\n        // Spec ToPropertyKey: pass ctx so ObjectLike keys dispatch through\n        // ToPrimitive(string) → ToString (test262 Object/defineProperty/\n        // 15.2.3.6-2-{20,24,25,39,41,43,44,45,46,47,48} pass arrays / boxed\n        // booleans / objects with overridden toString as the key arg).\n        CoreContext keyCtx = context instanceof CoreContext c ? c : null;\n        String prop = Terms.toPropertyKey(args[1], keyCtx);\n        Object desc = args[2];\n        ObjectLike descObj;\n        Map<String, Object> descMap;\n        if (desc instanceof ObjectLike ol) {\n            descObj = ol;\n            descMap = ol.toMap();\n        } else if (desc instanceof Map) {\n            descObj = null;\n            descMap = (Map<String, Object>) desc;\n        } else {\n            throw JsErrorException.typeError(\"Property descriptor must be an object\");","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java#L538-L574","documentation":"The third argument to Object.defineProperty must be a property descriptor object. Passing null or undefined as the descriptor throws this TypeError, per the spec's ToPropertyDescriptor which requires an Object.","triggerScenarios":"Object.defineProperty(obj, 'x', null), Object.defineProperty(obj, 'x', undefined), or a descriptor variable that failed to initialize (e.g. descriptors[key] returned undefined).","commonSituations":"Building descriptors dynamically from a lookup table with missing entries; API responses where the descriptor field is null; calling with only two arguments in a loosely-typed script.","solutions":["Provide a valid descriptor object, e.g. {value: ..., writable: true, enumerable: true, configurable: true}","Default an empty descriptor: desc ?? {value: undefined, writable: true, configurable: true}","Fix the descriptor lookup that returned null/undefined"],"exampleFix":"// before\nconst desc = descriptors[key]; // may be undefined\nObject.defineProperty(obj, key, desc); // throws\n// after\nconst desc = descriptors[key] ?? {value: undefined, writable: true, enumerable: true, configurable: true};\nObject.defineProperty(obj, key, desc);","handlingStrategy":"validation","validationCode":"if (desc == null) throw new TypeError('descriptor object required before defineProperty');","typeGuard":"function isDescriptor(d) { return d != null && typeof d === 'object'; }","tryCatchPattern":"try { Object.defineProperty(obj, key, desc); }\ncatch (e) { if (String(e).includes('descriptor must be an object')) { Object.defineProperty(obj, key, desc ?? {}); } else { throw e; } }","preventionTips":["Default missing descriptors to {} or a full data descriptor","Validate lookups that build descriptors dynamically","Never pass undefined/null as the third argument even in quick scripts"],"tags":["javascript","object","property-descriptor","null"],"backgroundTag":"null-argument","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"}