{"record":{"id":"453240c7b624aee9","repo":"karatelabs/karate","slug":"context-setglobal-requires-a-name-argument","errorCode":null,"errorMessage":"context.setGlobal() requires a name argument","messagePattern":"context\\.setGlobal\\(\\) requires a name argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java","lineNumber":373,"sourceCode":"                return null;\n            };\n            case \"uuid\" -> (JavaInvokable) args -> uuid();\n            case \"init\" -> (JavaInvokable) args -> {\n                init();\n                return session;  // Return session so scripts can do: session = context.init()\n            };\n            case \"close\" -> (JavaInvokable) args -> {\n                close();\n                return null;\n            };\n            case \"switch\" -> (JavaInvokable) args -> {\n                if (args.length == 0) throw new RuntimeException(\"switch() requires a template argument\");\n                switchTemplate(args[0].toString());\n                return null;\n            };\n            case \"setGlobal\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"context.setGlobal() requires a name argument\");\n                }\n                String name = args[0].toString();\n                Object value = args.length > 1 ? args[1] : null;\n                setGlobal(name, value);\n                return null;\n            };\n            case \"getGlobal\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"context.getGlobal() requires a name argument\");\n                }\n                String name = args[0].toString();\n                Object defaultValue = args.length > 1 ? args[1] : null;\n                return getGlobal(name, defaultValue);\n            };\n\n            // Properties\n            case \"ajax\" -> isAjax();\n            case \"sessionId\" -> session != null ? session.getId() : null;","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java#L355-L391","documentation":"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.","triggerScenarios":"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.","commonSituations":"Sharing state across requests in Karate mock servers (the testContextGlobalIsolatedAcrossRequests scenario exercises this); refactoring handlers where the name/value pair got swapped or dropped.","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."],"exampleFix":"// before\ncontext.setGlobal(userName); // name undefined/null\n// after\ncontext.setGlobal('currentUser', userName);","handlingStrategy":"validation","validationCode":"// JS in markup\nif (typeof name !== 'string' || !name) throw new Error('setGlobal requires a name');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["server","javascript","missing-argument","state"],"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"}