{"record":{"id":"ddea0428fce9b68e","repo":"karatelabs/karate","slug":"context-set-requires-a-name-argument","errorCode":null,"errorMessage":"context.set() requires a name argument","messagePattern":"context\\.set\\(\\) requires a name argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java","lineNumber":173,"sourceCode":"                    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\");\n                }\n                String name = args[0].toString();\n                Object value = args.length > 1 ? args[1] : null;\n                MarkupScope scope = getMarkupScope();\n                if (scope != null) {\n                    scope.set(name, value);\n                }\n                return null;\n            };\n            default -> null;\n        };\n    }\n\n}\n","sourceCodeStart":155,"sourceCodeEnd":188,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java#L155-L188","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a name as the first argument: context.set('myName', value)","If the intent is to share state across template renders in one request, use context.setGlobal instead","Check that the variable holding the key is not null/undefined before calling","Inside templates, prefer the direct `_.<name> = value` syntax which makes the key explicit"],"exampleFix":"// before\ncontext.set();\n// after\ncontext.set('cartTotal', total);","handlingStrategy":"validation","validationCode":"if (name == null || name.isEmpty()) throw new IllegalArgumentException(\"context.set requires a name\");\ncontext.set(name, value);","typeGuard":"function hasName(args) { return args.length > 0 && args[0] != null; }","tryCatchPattern":"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; }","preventionTips":["Always pass both name and value to context.set","Prefer `_.name = value` syntax in templates for clarity","Use context.setGlobal for cross-render state in server mode"],"tags":["javascript","markup-template","missing-argument"],"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"}