{"record":{"id":"76ea871784fe980e","repo":"karatelabs/karate","slug":"config-evaluation-failed-displayname-message","errorCode":null,"errorMessage":"Config evaluation failed: <displayName> - <message>","messagePattern":"Config evaluation failed: <displayName> - <message>","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/ScenarioRuntime.java","lineNumber":504,"sourceCode":"                // Already evaluated to a value (e.g., object literal)\n                result = fn;\n            }\n\n            // Apply config variables to engine\n            if (result instanceof Map) {\n                Map<String, Object> vars = (Map<String, Object>) result;\n                for (var entry : vars.entrySet()) {\n                    karate.engine.put(entry.getKey(), entry.getValue());\n                }\n                logger.debug(\"Evaluated {}: {} variables\", displayName, vars.size());\n                return vars;\n            } else if (result != null) {\n                logger.warn(\"{} did not return an object, got: {}\", displayName, result.getClass().getSimpleName());\n            }\n            return null;\n        } catch (Exception e) {\n            logger.warn(\"Failed to evaluate {}: {}\", displayName, e.getMessage());\n            throw new RuntimeException(\"Config evaluation failed: \" + displayName + \" - \" + e.getMessage(), e);\n        }\n    }\n\n    /**\n     * Execute the @setup scenario and return all its variables.\n     */\n    public Map<String, Object> executeSetup(String name) {\n        if (featureRuntime == null) {\n            throw new RuntimeException(\"karate.setup() requires a feature context\");\n        }\n        Scenario setupScenario = scenario.getFeature().getSetup(name);\n        if (setupScenario == null) {\n            String message = \"no scenario found with @setup tag\";\n            if (name != null) {\n                message = message + \" and name '\" + name + \"'\";\n            }\n            throw new RuntimeException(message);\n        }","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/ScenarioRuntime.java#L486-L522","documentation":"evalConfigJs evaluates the JS karate-config.js (or a JS config snippet) to initialize configuration variables. If the script throws or evaluation fails, the code logs a warning and wraps the exception in a RuntimeException naming the config source and the underlying message. The original exception is chained as the cause.","triggerScenarios":"karate-config.js throws a JS runtime error (undefined variable, typo, failed read(), calling karate.* APIs unavailable at config time), or configure() receives JS that fails to evaluate; invoked via evalConfig during ScenarioRuntime startup.","commonSituations":"Environment-specific config referencing a missing env variable; a read('my-utils.js') inside karate-config.js pointing to a bad path; upgrading Karate and relying on removed config APIs.","solutions":["Read the trailing part of the message (after 'displayName - ') — it contains the actual JS error and line.","Run the failing statement in isolation in karate-config.js and fix the JS error (undefined var, bad path).","Verify read(...) paths in the config resolve relative to the feature/classpath location.","Add defensive guards in karate-config.js (e.g. karate.env checks) so required inputs exist before use."],"exampleFix":"// before: karate-config.js\nvar port = java.lang.System.getenv('SERVICE_PORT');\nconfig.serverUrl = 'http://localhost:' + port.trim();\n\n// after\nvar port = java.lang.System.getenv('SERVICE_PORT');\nconfig.serverUrl = 'http://localhost:' + (port ? port.trim() : '8080');","handlingStrategy":"validation","validationCode":"// in karate-config.js, fail fast with clear messages\nfunction fn() {\n  var env = karate.env;\n  karate.log('karate.env:', env);\n  if (!env) throw 'karate.env not set; use -Dkarate.env=...';\n  var config = { env: env };\n  if (env == 'dev') config.serverUrl = 'http://localhost:8080';\n  else throw 'no config for env: ' + env;\n  return config;\n}","typeGuard":"function safeEnv(name) {\n  var v = java.lang.System.getenv(name);\n  if (v == null) throw 'required env var missing: ' + name;\n  return v;\n}","tryCatchPattern":"try {\n    ScenarioEngine.get().evalJs(configJs);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Config evaluation failed\")) {\n        throw new IllegalStateException(\"Check karate-config.js: \" + e.getCause(), e);\n    }\n    throw e;\n}","preventionTips":["Keep karate-config.js minimal and guarded — validate karate.env before branching","Test read() paths inside the config resolve relative to classpath root","After upgrading Karate, run one scenario early to smoke-test config evaluation","Avoid calling runtime-only karate.* APIs during config evaluation"],"tags":["javascript","configuration","karate-config"],"backgroundTag":"invalid-config-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"}