{"record":{"id":"10bdeb81d2de41ed","repo":"karatelabs/karate","slug":"context-get-requires-a-name-argument","errorCode":null,"errorMessage":"context.get() requires a name argument","messagePattern":"context\\.get\\(\\) requires a name argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java","lineNumber":155,"sourceCode":"            case \"toJson\" -> (JavaInvokable) args -> {\n                // null IS a valid JSON-able value (`null`), so only the\n                // missing-arg case errors here.\n                if (args.length == 0) throw new RuntimeException(\"toJson() requires an object argument\");\n                return toJson(args[0]);\n            };\n            case \"fromJson\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"fromJson() requires a JSON string argument\");\n                }\n                return fromJson(args[0].toString());\n            };\n            // context.get(name, default?) for optional fragment params.\n            // Walks the current eval scope (`_` map first, then wrapped\n            // Thymeleaf scope). Returns the bound non-null value if found,\n            // else the default (or null when no default is given).\n            case \"get\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"context.get() requires a name argument\");\n                }\n                String name = args[0].toString();\n                Object defaultValue = args.length > 1 ? args[1] : null;\n                MarkupScope scope = getMarkupScope();\n                if (scope != null) {\n                    Object v = scope.lookup(name);\n                    if (v != null) return v;\n                }\n                return defaultValue;\n            };\n            // context.set(name, value) — symmetric writer for context.get.\n            // Routes to MarkupScope.set, which writes to the `_` underscore\n            // namespace of the current render (same store as `_.<name> = value`\n            // from JS). Per-render lifetime; use context.setGlobal in server\n            // mode to cross template renders within a request.\n            case \"set\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"context.set() requires a name argument\");","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java#L137-L173","documentation":"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.","triggerScenarios":"Calling `context.get()` or `context.get(null)` in a template — name argument omitted, or a dynamic name variable that is null at render time.","commonSituations":"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.","solutions":["Pass a literal or non-null name: `context.get('title', 'Default Title')`.","Guard dynamic names: only call get when the name variable is non-null, or fall back to a fixed key.","Use the second argument to supply a default so absent params don't break rendering once the name is valid.","Inspect the fragment call site (`th:with`) to confirm which parameter names are actually passed."],"exampleFix":"// before\nvar v = context.get(paramName);\n\n// after\nvar v = paramName != null ? context.get(paramName, 'default') : 'default';","handlingStrategy":"validation","validationCode":"function safeGet(ctx, name, def) {\n  if (name == null || name === '') return def;\n  return ctx.get(name, def);\n}","typeGuard":"function hasName(n) { return n != null && typeof n === 'string' && n.length > 0; }\n// usage: if (hasName(n)) context.get(n, 'default');","tryCatchPattern":"try {\n  var v = context.get(name);\n} catch (e) {\n  if (e.message === 'context.get() requires a name argument') {\n    v = null;\n  } else { throw e; }\n}","preventionTips":["Always pass a non-null parameter name to context.get","Use the second argument to supply defaults for optional fragment params","Guard dynamically computed names before lookup","Verify fragment call sites (th:with) actually pass the params you read"],"tags":["markup","javascript-binding","missing-argument","fragments"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}