karatelabs/karate · error · RuntimeException
context.setGlobal() requires a name argument
Error message
context.setGlobal() requires a name argument
What it means
context.setGlobal(name, value) stores a variable in the server-global (cross-request) scope and rejects calls without a non-null name argument with this RuntimeException. The name is required because globals are keyed by it.
Solutions
- Pass a non-null string name first: context.setGlobal('key', value).
- Check argument order — name must be args[0]; value second (optional).
- Guard the name variable before the call.
- If the value is what's missing, note value is optional (defaults to null); only the name is required.
Example fix
// before
context.setGlobal(userName); // name undefined/null
// after
context.setGlobal('currentUser', userName); Defensive patterns
Strategy: validation
Validate before calling
// JS in markup
if (typeof name !== 'string' || !name) throw new Error('setGlobal requires a name'); Prevention
- Pass name first, value second: setGlobal('key', value)
- Use string-literal keys where possible
- Guard variables that supply the key
- Document global keys used across handlers
When it happens
Trigger: Calling context.setGlobal() with no arguments or with null as the first argument in server markup JavaScript — commonly when the variable holding the name is undefined or when arguments are passed in the wrong order.
Common situations: Sharing state across requests in Karate mock servers (the testContextGlobalIsolatedAcrossRequests scenario exercises this); refactoring handlers where the name/value pair got swapped or dropped.
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.getGlobal() 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/453240c7b624aee9.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java:373
return null;
};
case "uuid" -> (JavaInvokable) args -> uuid();
case "init" -> (JavaInvokable) args -> {
init();
return session; // Return session so scripts can do: session = context.init()
};
case "close" -> (JavaInvokable) args -> {
close();
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;View on GitHub (pinned to a22eb90246)