{"record":{"id":"2fcd2a30a95141e8","repo":"karatelabs/karate","slug":"scenario-match-evaluation-failed-at-line","errorCode":null,"errorMessage":"scenario match evaluation failed at line {}: {} - {}","messagePattern":"scenario match evaluation failed at line (.+?): (.+?) - (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"karate-core/src/main/java/io/karatelabs/core/MockHandler.java","lineNumber":482,"sourceCode":"        String expression = StringUtils.trimToNull(scenario.getName());\n\n        // Empty/null expression means catch-all (always matches)\n        if (expression == null) {\n            logger.debug(\"catch-all scenario matched at line: {}\", scenario.getLine());\n            return true;\n        }\n\n        try {\n            Object result = engine.eval(expression);\n            if (Boolean.TRUE.equals(result)) {\n                logger.debug(\"scenario matched at line {}: {}\", scenario.getLine(), expression);\n                return true;\n            } else {\n                logger.trace(\"scenario skipped at line {}: {}\", scenario.getLine(), expression);\n                return false;\n            }\n        } catch (Exception e) {\n            logger.warn(\"scenario match evaluation failed at line {}: {} - {}\", scenario.getLine(), expression, e.getMessage());\n            return false;\n        }\n    }\n\n    private HttpResponse executeScenario(ScenarioRuntime runtime, Scenario scenario, HttpRequest request) {\n        Engine engine = runtime.getEngine();\n        StepExecutor executor = new StepExecutor(runtime);\n\n        // Execute beforeScenario hook before step execution so request-scoped setup can run per-request.\n        // A hook exception surfaces as HTTP 500 (same as a step failure) - wrap the hook body in\n        // try/catch if you want to suppress errors.\n        Exception beforeError = invokeMockHook(config.getBeforeScenario(), \"beforeScenario\");\n        if (beforeError != null) {\n            return hookErrorResponse(\"beforeScenario\", beforeError);\n        }\n\n        // Execute all steps in the scenario using StepExecutor\n        for (Step step : scenario.getSteps()) {","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/MockHandler.java#L464-L500","documentation":"While deciding whether a mock scenario applies to an incoming request, MockHandler evaluates the scenario's match expression through the JS engine. If that evaluation throws (bad expression syntax, missing/undefined variable used in the match, engine failure), the exception is caught and logged with this warning and the scenario is treated as not matching — the request moves on to other scenarios and eventually 404.","triggerScenarios":"A mock feature scenario contains a match expression (e.g. `pathMatches('/x') && paramValue('q') == someVar`) that references an undefined variable, calls a function that throws, or has a runtime error; isMatchingScenario's try block throws Exception and the warn branch fires.","commonSituations":"Typo in a variable name inside the match expression; using `def`-defined variables only in later scenarios instead of Background; calling helper JS that itself fails (null deref, missing function); copy-paste errors after renaming variables.","solutions":["Read the logged line number and expression in the warning; run that expression's logic in a Karate test or JS REPL to see the real exception.","Define all variables used in match expressions in Background or at the top of the scenario so they exist at evaluation time.","Simplify the match expression (split `&&` clauses) to isolate which part throws.","Enable trace logging to see which scenarios are skipped vs which failed evaluation."],"exampleFix":"// before: undefined variable in expression\nScenario: users\n  * pathMatches('/users/' + userId)\n// after: define it in Background\nBackground:\n  * def userId = 1\nScenario: users\n  * pathMatches('/users/' + userId)","handlingStrategy":"try-catch","validationCode":"// Pre-validate that variables used in match expressions exist\nfor (String var : List.of(\"userId\", \"expectedStatus\")) {\n    if (engine.getVariable(var) == null) {\n        throw new IllegalStateException(\"match expression variable missing: \" + var);\n    }\n}","typeGuard":null,"tryCatchPattern":"// Wrap mock startup/request handling so eval failures surface immediately\ntry {\n    HttpResponse res = handler.response(request);\n} catch (Exception e) {\n    logger.error(\"mock request handling failed\", e);\n    throw e;\n}","preventionTips":["Define every variable referenced in match expressions in Background","Keep match expressions small; move complex logic into def'd helper functions that are themselves tested","Avoid calling request-derived helpers (paramValue, header) on values that may be absent — guard for null first","Reproduce the failing expression in a scenario executed by the normal runner to get the full stack trace"],"tags":["mock-server","expression-evaluation","javascript","scenario-matching"],"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"}