{"record":{"id":"0d2d2ba1c14e247c","repo":"karatelabs/karate","slug":"onstderr-requires-a-function-argument","errorCode":null,"errorMessage":"onStdErr requires a function argument","messagePattern":"onStdErr requires a function argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/process/ProcessHandle.java","lineNumber":512,"sourceCode":"                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 {\n                        listener.call(ctx, line);\n                    } catch (Exception e) {\n                        logger.warn(\"onStdErr listener error: {}\", e.getMessage());\n                    }\n                });\n                return this;\n            };\n            case \"waitSync\" -> (JavaCallable) (ctx, args) -> {\n                if (args.length > 0 && args[0] instanceof Number) {\n                    return waitSync(((Number) args[0]).longValue());\n                }\n                return waitSync();\n            };\n            case \"waitForOutput\" -> (JavaCallable) (ctx, args) -> {","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/process/ProcessHandle.java#L494-L530","documentation":"ProcessHandle.onStdErr() registers a JS callback invoked for each stderr line of the managed process. The binding validates that the first argument is a function; a missing or non-function argument throws this RuntimeException.","triggerScenarios":"Calling process.onStdErr() with no arguments, or passing any non-callable value (string, number, plain object) as the first argument.","commonSituations":"Copy-paste from onStdOut where the callback was not defined; passing an object with a method instead of the function itself; accidentally passing null when the listener is optional.","solutions":["Pass an actual function: p.onStdErr(function(line){ ... })","Guard optional listeners: only call onStdErr when (typeof listener === 'function')","Remember the callback receives the raw line string"],"exampleFix":"// before\np.onStdErr(logger.error); // logger.error may be unbound / not a function in JS scope\n// after\np.onStdErr(function(line){ karate.log('stderr: ' + line); });","handlingStrategy":"type-guard","validationCode":"if (listener && typeof listener !== 'function') { throw new Error('stderr listener must be a function'); }","typeGuard":"function isFn(x) { return typeof x === 'function'; }","tryCatchPattern":"try {\n  p.onStdErr(listener);\n} catch (e) {\n  karate.log('onStdErr registration failed: ' + e.message);\n}","preventionTips":["Only call onStdErr with an actual JS function","Guard optional listeners with typeof checks","Copy-paste onStdOut blocks carefully — argument contract is identical"],"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"}