{"record":{"id":"f1746a389e50245c","repo":"karatelabs/karate","slug":"object-defineproperty-called-on-non-object","errorCode":null,"errorMessage":"Object.defineProperty called on non-object","messagePattern":"Object\\.defineProperty called on non-object","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java","lineNumber":550,"sourceCode":"        }\n        desc.put(\"enumerable\", slot.isEnumerable());\n        desc.put(\"configurable\", slot.isConfigurable());\n        return new JsObject(desc);\n    }\n\n    /** 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;","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsObjectConstructor.java#L532-L568","documentation":"Object.defineProperty requires its first argument to be an object (ObjectLike) or a Map in this engine. Passing null, undefined, a primitive, or any other non-object type throws this TypeError, matching the spec's RequireObjectCoercible + Object check behavior.","triggerScenarios":"Object.defineProperty(null, 'x', {}), Object.defineProperty(undefined, ...), Object.defineProperty(42, ...), or a variable holding null because an object lookup/factory returned nothing.","commonSituations":"Chained lookups like defineProperty(api?.config, ...) where config is missing; passing a JSON.parse result that came back null; refactoring left a primitive where an object was expected.","solutions":["Ensure the target is an object before calling: if (!target || typeof target !== 'object') create or load it first","Fix the upstream code that produced null/undefined","Use a Map if you intentionally want a key-value container (Map targets are accepted)"],"exampleFix":"// before\nconst target = config[objName]; // may be undefined\nObject.defineProperty(target, 'x', {value: 1}); // throws if undefined\n// after\nconst target = config[objName] ?? {};\nObject.defineProperty(target, 'x', {value: 1});","handlingStrategy":"type-guard","validationCode":"if (target == null || (typeof target !== 'object' && typeof target !== 'function')) throw new TypeError('defineProperty target must be an object');","typeGuard":"function isDefineTarget(t) { return t != null && (typeof t === 'object' || typeof t === 'function'); }","tryCatchPattern":"try { Object.defineProperty(target, key, desc); }\ncatch (e) { if (String(e).includes('called on non-object')) { target = {}; Object.defineProperty(target, key, desc); } else { throw e; } }","preventionTips":["Guard optional-chained targets (api?.config) before defineProperty","Default null lookups to {} instead of passing them through","Remember primitives are rejected — box explicitly if you need property metadata on them"],"tags":["javascript","object","type-error","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"}