{"record":{"id":"1b05a9d602ac90a8","repo":"karatelabs/karate","slug":"reflect-ownkeys-called-on-non-object","errorCode":null,"errorMessage":"Reflect.ownKeys called on non-object","messagePattern":"Reflect\\.ownKeys called on non-object","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsReflect.java","lineNumber":52,"sourceCode":"class JsReflect extends JsObject {\n\n    @Override\n    protected Object resolveOwnIntrinsic(String name) {\n        return switch (name) {\n            case \"construct\" -> (JsCallable) this::construct;\n            case \"apply\" -> (JsCallable) this::apply;\n            case \"ownKeys\" -> (JsCallable) this::ownKeys;\n            default -> null;\n        };\n    }\n\n    private static final List<String> INTRINSIC_NAMES = List.of(\"construct\", \"apply\", \"ownKeys\");\n\n    /** §28.1.11 — [[OwnPropertyKeys]] in spec order: string keys, then the\n     *  symbol values from the object's symbol store. */\n    private Object ownKeys(Context context, Object[] args) {\n        if (args.length < 1 || !(args[0] instanceof ObjectLike target)) {\n            throw JsErrorException.typeError(\"Reflect.ownKeys called on non-object\");\n        }\n        // §10.1.11.1 OrdinaryOwnPropertyKeys: integer indices ascending, then\n        // strings in insertion order — the same helper Object.keys uses.\n        // named half only for an array — toMap()'s index keys include holes,\n        // and the spec index+length key set for arrays is a separate gap here\n        List<Object> keys = new java.util.ArrayList<>(JsObject.orderedOwnKeys(\n                target instanceof JsArray ja ? ja.namedPropsView().keySet() : target.toMap().keySet()));\n        if (target instanceof JsObject jo) {\n            keys.addAll(jo.ownSymbols());\n        }\n        return new JsArray(keys);\n    }\n\n    @Override\n    protected Iterable<String> ownIntrinsicNames() {\n        return INTRINSIC_NAMES;\n    }\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsReflect.java#L34-L70","documentation":"`Reflect.ownKeys(target)` implements [[OwnPropertyKeys]] and, per spec, requires an Object target. Karate's `ownKeys` throws this TypeError when the first argument is missing or not an ObjectLike (a string, number, null, undefined, etc.).","triggerScenarios":"`Reflect.ownKeys()` with no args; `Reflect.ownKeys('abc')`, `Reflect.ownKeys(42)`, `Reflect.ownKeys(null)`; passing a Java-side non-object value into the JS call.","commonSituations":"Reflected/introspective code fed by dynamic data where the value unexpectedly became a primitive; forwarding undefined from an earlier failed lookup.","solutions":["Pass an actual object: `Reflect.ownKeys(obj)`.","Guard beforehand: `if (target && typeof target === 'object') Reflect.ownKeys(target)`.","For primitives, use `Object.keys(Object(primitive))` semantics explicitly if that's the intent."],"exampleFix":"// before\nconst keys = Reflect.ownKeys(maybeObject); // may be undefined\n// after\nconst keys = maybeObject != null && typeof maybeObject === 'object' ? Reflect.ownKeys(maybeObject) : [];","handlingStrategy":"type-guard","validationCode":"function ownKeysSafe(target) {\n  if (target == null || (typeof target !== 'object' && typeof target !== 'function')) return [];\n  return Reflect.ownKeys(target);\n}","typeGuard":"function isReflectTarget(v) { return v != null && (typeof v === 'object' || typeof v === 'function'); }","tryCatchPattern":"try { return Reflect.ownKeys(target); } catch (e) { if (e instanceof TypeError && /ownKeys called on non-object/.test(e.message)) return []; throw e; }","preventionTips":["Validate dynamic targets before reflective calls.","Coerce primitives explicitly with Object(v) if reflection is intended.","Check for undefined coming from failed lookups upstream."],"tags":["javascript","reflect","type-check"],"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"}