karatelabs/karate · error · RuntimeException

context.get() requires a name argument

Error message

context.get() requires a name argument

What it means

MarkupContext exposes a JS-callable `context.get(name, default?)` for reading optional fragment parameters; it walks the current eval scope (`_` map first, then the wrapped Thymeleaf scope). When called with no arguments or a null first argument, the wrapper throws this RuntimeException since there is no parameter name to look up.

Solutions

  1. Pass a literal or non-null name: `context.get('title', 'Default Title')`.
  2. Guard dynamic names: only call get when the name variable is non-null, or fall back to a fixed key.
  3. Use the second argument to supply a default so absent params don't break rendering once the name is valid.
  4. Inspect the fragment call site (`th:with`) to confirm which parameter names are actually passed.

Example fix

// before
var v = context.get(paramName);

// after
var v = paramName != null ? context.get(paramName, 'default') : 'default';
Defensive patterns

Strategy: validation

Validate before calling

function safeGet(ctx, name, def) {
  if (name == null || name === '') return def;
  return ctx.get(name, def);
}

Type guard

function hasName(n) { return n != null && typeof n === 'string' && n.length > 0; }
// usage: if (hasName(n)) context.get(n, 'default');

Try / catch

try {
  var v = context.get(name);
} catch (e) {
  if (e.message === 'context.get() requires a name argument') {
    v = null;
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling `context.get()` or `context.get(null)` in a template — name argument omitted, or a dynamic name variable that is null at render time.

Common situations: Dynamically computed fragment-param names that resolve to null when the fragment isn't invoked with that parameter; missing argument after refactor; using get() to probe optional params but building the name from an unset value.

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/10bdeb81d2de41ed. Report an issue: GitHub.

Appendix: source

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

            case "toJson" -> (JavaInvokable) args -> {
                // null IS a valid JSON-able value (`null`), so only the
                // missing-arg case errors here.
                if (args.length == 0) throw new RuntimeException("toJson() requires an object argument");
                return toJson(args[0]);
            };
            case "fromJson" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("fromJson() requires a JSON string argument");
                }
                return fromJson(args[0].toString());
            };
            // context.get(name, default?) for optional fragment params.
            // Walks the current eval scope (`_` map first, then wrapped
            // Thymeleaf scope). Returns the bound non-null value if found,
            // else the default (or null when no default is given).
            case "get" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    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");

View on GitHub (pinned to a22eb90246)