karatelabs/karate · error · RuntimeException
context.getGlobal() requires a name argument
Error message
context.getGlobal() requires a name argument
What it means
context.getGlobal(name, defaultValue) reads a server-global variable and requires a non-null name argument; otherwise this RuntimeException is thrown. An optional second argument supplies a default when the key is absent, but the key itself must always be present.
Solutions
- Pass the key explicitly: context.getGlobal('key').
- Supply a default as the second argument instead of omitting the key: getGlobal('key', fallback).
- Guard the key variable before the call.
- If you need all globals, there is no such API — track keys separately in a global object.
Example fix
// before var v = context.getGlobal(key); // key undefined // after var v = context.getGlobal(key || 'defaultKey', null);
Defensive patterns
Strategy: validation
Validate before calling
// JS in markup if (typeof key !== 'string' || !key) key = 'defaultKey';
Prevention
- Always pass the key; use the second argument for a default value
- Don't treat getGlobal() as a bulk getter
- Guard keys derived from request parameters
- Centralize global key names as constants
When it happens
Trigger: Calling context.getGlobal() with zero arguments or null first argument in server markup JavaScript — usually an undefined key variable, or assuming getGlobal() with no args returns all globals.
Common situations: Reading cross-request state in mock-server handlers; retrieving a key whose name is computed from request parameters that are missing; confusing getGlobal() with a bulk getter.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- context.setGlobal() requires a name argument
- redirect() requires a URL argument
- switch() requires a template argument
- doc() requires 'read' key with template path
- doc() needs at least one argument
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/ea1137a04b542088.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java:382
return null;
};
case "switch" -> (JavaInvokable) args -> {
if (args.length == 0) throw new RuntimeException("switch() requires a template argument");
switchTemplate(args[0].toString());
return null;
};
case "setGlobal" -> (JavaInvokable) args -> {
if (args.length == 0 || args[0] == null) {
throw new RuntimeException("context.setGlobal() requires a name argument");
}
String name = args[0].toString();
Object value = args.length > 1 ? args[1] : null;
setGlobal(name, value);
return null;
};
case "getGlobal" -> (JavaInvokable) args -> {
if (args.length == 0 || args[0] == null) {
throw new RuntimeException("context.getGlobal() requires a name argument");
}
String name = args[0].toString();
Object defaultValue = args.length > 1 ? args[1] : null;
return getGlobal(name, defaultValue);
};
// Properties
case "ajax" -> isAjax();
case "sessionId" -> session != null ? session.getId() : null;
case "closed" -> isClosed();
case "switched" -> switched;
case "flash" -> flash;
case "actions" -> actionsView();
case "request" -> request;
case "response" -> response;
case "session" -> session;
case "csrf" -> getCsrfToken();
View on GitHub (pinned to a22eb90246)