{"record":{"id":"963a641e10bfa9b6","repo":"karatelabs/karate","slug":"reflect-apply-target-is-not-callable","errorCode":null,"errorMessage":"Reflect.apply: target is not callable","messagePattern":"Reflect\\.apply: target is not callable","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsReflect.java","lineNumber":101,"sourceCode":"        if (args.length >= 3) {\n            if (!(args[2] instanceof JsCallable nt)) {\n                throw JsErrorException.typeError(\"Reflect.construct: newTarget is not a constructor\");\n            }\n            check = nt;\n        }\n        if (!check.isConstructable()) {\n            throw JsErrorException.typeError(\"Reflect.construct: target is not a constructor\");\n        }\n        if (!(context instanceof CoreContext cc)) {\n            throw JsErrorException.typeError(\"Reflect.construct: bad context\");\n        }\n        return Interpreter.constructFromHost(target, cArgs, cc);\n    }\n\n    // Reflect.apply(target, thisArgument, argumentsList) — spec §28.1.1.\n    private Object apply(Context context, Object[] args) {\n        if (args.length < 1 || !(args[0] instanceof JsCallable target)) {\n            throw JsErrorException.typeError(\"Reflect.apply: target is not callable\");\n        }\n        Object thisArg = args.length >= 2 ? args[1] : Terms.UNDEFINED;\n        Object[] cArgs = listToArray(args.length >= 3 ? args[2] : null);\n        if (context instanceof CoreContext cc) {\n            cc.thisObject = thisArg;\n        }\n        return target.call(context, cArgs);\n    }\n\n    private static Object[] listToArray(Object value) {\n        if (value == null || value == Terms.UNDEFINED) {\n            return new Object[0];\n        }\n        if (value instanceof List<?> list) {\n            return list.toArray();\n        }\n        if (value instanceof Object[] arr) {\n            return arr;","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsReflect.java#L83-L119","documentation":"Reflect.apply(target, thisArgument, argumentsList) requires the first argument to be a callable function (spec §28.1.1). Karate's JsReflect throws this TypeError when args is empty or args[0] is not a JsCallable — e.g. a number, string, object, or undefined was passed as the target.","triggerScenarios":"Reflect.apply() with no arguments; Reflect.apply(fnName, ...) where fnName is undefined due to a typo or failed import; passing a non-callable like an object, string, or number as the first argument.","commonSituations":"Dynamic function lookup that returns undefined at runtime; refactoring that renamed a function but missed a Reflect.apply call site; passing `this` or a value from a lookup table that holds non-function members.","solutions":["Check the first argument is a function before calling: `typeof fn === 'function'`","Fix typos/undefined variables in the target position","Use Function.prototype.call/bind or `fn(...args)` only on confirmed callables"],"exampleFix":"// before\nReflect.apply(handlers[name], this, args); // handlers[name] may be undefined\n// after\nif (typeof handlers[name] !== 'function') throw new Error('no handler: ' + name);\nReflect.apply(handlers[name], this, args);","handlingStrategy":"type-guard","validationCode":"if (typeof target !== 'function') throw new Error('Reflect.apply requires a callable target, got: ' + target);","typeGuard":"const isCallable = v => typeof v === 'function';\nif (!isCallable(target)) throw new Error('not callable: ' + target);","tryCatchPattern":"try {\n  return Reflect.apply(target, thisArg, args);\n} catch (e) {\n  if (String(e.message).includes('not callable')) {\n    throw new Error('Reflect.apply target is not a function: ' + String(target));\n  }\n  throw e;\n}","preventionTips":["Check typeof fn === 'function' on dynamically resolved function names","Watch for undefined targets after renames/imports changes","Validate lookup-table handlers are all functions at startup","Prefer fn.apply/fn.call directly so missing functions fail at the same site"],"tags":["javascript","reflect","typeerror","callable"],"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-16T19:17:19.609Z"}