karatelabs/karate · error · RuntimeException

context.set() requires a name argument

Error message

context.set() requires a name argument

What it means

karate-core throws this when JS template code calls context.set() with no arguments (or a null first argument). context.set(name, value) routes to MarkupScope.set, which stores a value in the per-render `_` underscore namespace of the current render. The library requires an explicit name key for every stored value.

Solutions

  1. Pass a name as the first argument: context.set('myName', value)
  2. If the intent is to share state across template renders in one request, use context.setGlobal instead
  3. Check that the variable holding the key is not null/undefined before calling
  4. Inside templates, prefer the direct `_.<name> = value` syntax which makes the key explicit

Example fix

// before
context.set();
// after
context.set('cartTotal', total);
Defensive patterns

Strategy: validation

Validate before calling

if (name == null || name.isEmpty()) throw new IllegalArgumentException("context.set requires a name");
context.set(name, value);

Type guard

function hasName(args) { return args.length > 0 && args[0] != null; }

Try / catch

try { context.set(name, value); } catch (RuntimeException e) { if (e.getMessage().contains("requires a name")) { log.warn("missing name for context.set"); } else throw e; }

Prevention

When it happens

Trigger: Calling context.set() with zero args or context.set(null) from JS inside a markup template render; calling context.set(value) forgetting the name first; dynamic name that evaluates to null at call time.

Common situations: Template authors confusing context.set(name, value) with a value-only setter; refactors that drop the first argument; server-mode users who should have used context.setGlobal to persist across template renders within a request.

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


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/ddea0428fce9b68e. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java:173

                    throw new RuntimeException("context.get() requires a name argument");
                }
                String name = args[0].toString();
                Object defaultValue = args.length > 1 ? args[1] : null;
                MarkupScope scope = getMarkupScope();
                if (scope != null) {
                    Object v = scope.lookup(name);
                    if (v != null) return v;
                }
                return defaultValue;
            };
            // context.set(name, value) — symmetric writer for context.get.
            // Routes to MarkupScope.set, which writes to the `_` underscore
            // namespace of the current render (same store as `_.<name> = value`
            // from JS). Per-render lifetime; use context.setGlobal in server
            // mode to cross template renders within a request.
            case "set" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("context.set() requires a name argument");
                }
                String name = args[0].toString();
                Object value = args.length > 1 ? args[1] : null;
                MarkupScope scope = getMarkupScope();
                if (scope != null) {
                    scope.set(name, value);
                }
                return null;
            };
            default -> null;
        };
    }

}

View on GitHub (pinned to a22eb90246)