{"record":{"id":"fee7795673728a0f","repo":"karatelabs/karate","slug":"onstdout-requires-a-function-argument","errorCode":null,"errorMessage":"onStdOut requires a function argument","messagePattern":"onStdOut requires a function argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/process/ProcessHandle.java","lineNumber":498,"sourceCode":"    public Collection<String> jsKeys() {\n        return KEYS;\n    }\n\n    @Override\n    public Object jsGet(String key) {\n        return switch (key) {\n            case \"stdOut\" -> getStdOut();\n            case \"stdErr\" -> getStdErr();\n            case \"exitCode\" -> getExitCode();\n            case \"alive\" -> isAlive();\n            case \"pid\" -> getPid();\n            case \"start\" -> (JavaCallable) (ctx, args) -> {\n                start();\n                return this;\n            };\n            case \"onStdOut\" -> (JavaCallable) (ctx, args) -> {\n                if (args.length == 0 || !(args[0] instanceof JavaCallable)) {\n                    throw new RuntimeException(\"onStdOut requires a function argument\");\n                }\n                JavaCallable listener = (JavaCallable) args[0];\n                onStdOut(line -> {\n                    try {\n                        listener.call(ctx, line);\n                    } catch (Exception e) {\n                        logger.warn(\"onStdOut listener error: {}\", e.getMessage());\n                    }\n                });\n                return this;\n            };\n            case \"onStdErr\" -> (JavaCallable) (ctx, args) -> {\n                if (args.length == 0 || !(args[0] instanceof JavaCallable)) {\n                    throw new RuntimeException(\"onStdErr requires a function argument\");\n                }\n                JavaCallable listener = (JavaCallable) args[0];\n                onStdErr(line -> {\n                    try {","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/process/ProcessHandle.java#L480-L516","documentation":"ProcessHandle.onStdOut() registers a JS callback invoked for each stdout line of the managed process. The library requires exactly that callback: calling onStdOut with no argument or with a non-function value is rejected at binding time with this RuntimeException.","triggerScenarios":"Calling process.onStdOut() with zero arguments, or passing a string/number/object instead of a JS function (e.g. onStdOut('handleLine') or onStdOut(someObject)).","commonSituations":"Typos where the intended callback variable is undefined or null; porting Java-style code that passes a method reference as a string; forgetting to define the listener before registering it.","solutions":["Pass a JS function literal: karate.process(...).onStdOut(function(line){ ... })","Verify the callback variable is defined and is a function, not a string naming it","Register the listener before start() if you want to capture early output"],"exampleFix":"// before\nvar p = karate.start(...);\np.onStdOut('logLine');\n// after\nvar p = karate.start(...);\np.onStdOut(line => karate.log('stdout: ' + line));","handlingStrategy":"type-guard","validationCode":"if (typeof listener !== 'function') { throw new Error('listener must be a function'); }","typeGuard":"function isFn(x) { return typeof x === 'function'; }","tryCatchPattern":"try {\n  p.onStdOut(listener);\n} catch (e) {\n  karate.log('onStdOut registration failed: ' + e.message);\n}","preventionTips":["Define the callback as a function literal at the call site","Never pass a string method name where a function is expected","Register listeners before start()"],"tags":["process","javascript","callback","argument-type"],"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"}