{"record":{"id":"39eab62769cd9f0c","repo":"karatelabs/karate","slug":"failed-to-evaluate-dynamic-expression-templatescenario","errorCode":null,"errorMessage":"Failed to evaluate dynamic expression: ${templateScenario.getDynamicExpression()}","messagePattern":"Failed to evaluate dynamic expression: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/FeatureRuntime.java","lineNumber":753,"sourceCode":"                String expression = templateScenario.getDynamicExpression();\n                Object result;\n                try {\n                    result = sr.eval(expression);\n                } finally {\n                    sr.releaseHttpClient();\n                }\n\n                if (result instanceof List) {\n                    return (List<?>) result;\n                } else if (result instanceof JavaCallable) {\n                    // Generator function - call repeatedly until null/non-map\n                    return evaluateGeneratorFunction(result);\n                } else {\n                    // Expression didn't return a list or function - error\n                    throw new RuntimeException(\"Dynamic expression must return a list or function: \" + expression + \", got: \" + (result != null ? result.getClass().getName() : \"null\"));\n                }\n            } catch (Exception e) {\n                throw new RuntimeException(\"Failed to evaluate dynamic expression: \" + templateScenario.getDynamicExpression(), e);\n            }\n        }\n\n        /**\n         * Evaluates a generator function by calling it repeatedly with incrementing index\n         * until it returns null or a non-Map value.\n         */\n        @SuppressWarnings(\"unchecked\")\n        private List<Map<String, Object>> evaluateGeneratorFunction(Object function) {\n            List<Map<String, Object>> results = new ArrayList<>();\n            JavaCallable callable = (JavaCallable) function;\n            int index = 0;\n\n            while (true) {\n                Object rowValue;\n                try {\n                    // JsCallable.call() works with null context (uses declared context internally)\n                    rowValue = callable.call(null, index);","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/FeatureRuntime.java#L735-L771","documentation":"Karate wraps any exception thrown while evaluating a dynamic scenario expression in this RuntimeException, preserving the original as the cause. It signals that the expression itself failed to evaluate (syntax error, missing variable, thrown exception inside the called function), as opposed to returning a wrong type.","triggerScenarios":"A dynamic expression in an Examples table throws during evaluation — undefined JS variable, a Java method that throws, malformed JS syntax, or a generator function raising an exception mid-iteration.","commonSituations":"Calling a helper defined in another feature that isn't in scope; a typo in a variable name inside the expression; a Java call whose signature changed; classpath missing for a Java class referenced by the expression.","solutions":["Inspect the cause exception in the stack trace to find the underlying JS/Java failure","Verify all variables and functions referenced by the expression are defined in scope (def before the Examples)","Run the expression's body as a normal '* def result = <expr>' step to reproduce the error in isolation","Fix the underlying exception (missing def, wrong signature, missing class) and re-run"],"exampleFix":"// before (undefined helper)\nExamples: karate.call('missing.feature')\n// after\n* def rows = call read('helpers.feature')\nExamples: rows","handlingStrategy":"try-catch","validationCode":"// reproduce the expression as a plain step first\n* def __probe = <expression>","typeGuard":"null","tryCatchPattern":"try { evaluateDynamic(expression) } catch (RuntimeException e) { Throwable cause = e.getCause(); log.error('Dynamic expression failed: ' + cause, cause); throw e; }","preventionTips":["Define all variables/functions used by the expression before the Examples table","Test the expression standalone as a '* def' step while developing","Keep Java helpers called by expressions stable and on the test classpath"],"tags":["dynamic-scenarios","examples-table","evaluation-failed","javascript"],"backgroundTag":"api-request-failed","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"}