karatelabs/karate · error · RuntimeException
Failed to evaluate
Error message
Failed to evaluate {}: {} What it means
Evaluating the karate config JavaScript (karate-config.js or a named variant) threw an exception. Karate logs the message and then rethrows a RuntimeException "Config evaluation failed: <displayName> - <reason>", aborting scenario startup. This is a hard failure, unlike the non-object return case.
Solutions
- Read the '{}' detail in the log/exception — the underlying JS error — and fix that line in the config script.
- Run the config body in a plain JS engine or node to reproduce and debug the error.
- Guard environment-dependent code: `var env = karate.env || 'dev';` and null-check system properties.
- Keep a minimal known-good karate-config.js and bisect additions until the failure disappears.
Example fix
// before
var key = java.lang.System.getProperty('secret.key').trim();
// after
var key = java.lang.System.getProperty('secret.key');
if (!key) key = 'default'; Defensive patterns
Strategy: try-catch
Validate before calling
// smoke-test the config before running suites
karate.env = karate.env || 'dev';
if (!karate.env) throw new Error('karate.env missing'); Try / catch
try { vars = evalConfigJs(...); } catch (e) { throw new RuntimeException("Config evaluation failed: " + displayName + " - " + e.getMessage(), e); } Prevention
- Guard against null/undefined karate.env and system properties.
- Keep config scripts small; move complex logic into tested JS/Java helpers.
- Version-control and review karate-config.js changes like production code.
When it happens
Trigger: JS syntax error or runtime exception (undefined variable, calling missing function, etc.) inside karate-config.js / evalConfigJs while evalConfig initializes the scenario context.
Common situations: karate.env not handled (switch on undefined); typo in karate.* API usage; reading a system property that is null and calling methods on it; broken config after refactor or merge conflict.
Related errors
- a class declaration may not be the body of
- a function declaration may not be the body of
- a lexical declaration may not be the body of
- 'await' is a reserved word in a class static initialization…
- 'break' may not cross a class static initialization block
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/5c6c6e82e121573b.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/ScenarioRuntime.java:503
} else {
// Already evaluated to a value (e.g., object literal)
result = fn;
}
// Apply config variables to engine
if (result instanceof Map) {
Map<String, Object> vars = (Map<String, Object>) result;
for (var entry : vars.entrySet()) {
karate.engine.put(entry.getKey(), entry.getValue());
}
logger.debug("Evaluated {}: {} variables", displayName, vars.size());
return vars;
} else if (result != null) {
logger.warn("{} did not return an object, got: {}", displayName, result.getClass().getSimpleName());
}
return null;
} catch (Exception e) {
logger.warn("Failed to evaluate {}: {}", displayName, e.getMessage());
throw new RuntimeException("Config evaluation failed: " + displayName + " - " + e.getMessage(), e);
}
}
/**
* Execute the @setup scenario and return all its variables.
*/
public Map<String, Object> executeSetup(String name) {
if (featureRuntime == null) {
throw new RuntimeException("karate.setup() requires a feature context");
}
Scenario setupScenario = scenario.getFeature().getSetup(name);
if (setupScenario == null) {
String message = "no scenario found with @setup tag";
if (name != null) {
message = message + " and name '" + name + "'";
}
throw new RuntimeException(message);View on GitHub (pinned to a22eb90246)