{"record":{"id":"ea1137a04b542088","repo":"karatelabs/karate","slug":"context-getglobal-requires-a-name-argument","errorCode":null,"errorMessage":"context.getGlobal() requires a name argument","messagePattern":"context\\.getGlobal\\(\\) requires a name argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java","lineNumber":382,"sourceCode":"                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;\n            case \"closed\" -> isClosed();\n            case \"switched\" -> switched;\n            case \"flash\" -> flash;\n            case \"actions\" -> actionsView();\n            case \"request\" -> request;\n            case \"response\" -> response;\n            case \"session\" -> session;\n            case \"csrf\" -> getCsrfToken();\n","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java#L364-L400","documentation":"context.getGlobal(name, defaultValue) reads a server-global variable and requires a non-null name argument; otherwise this RuntimeException is thrown. An optional second argument supplies a default when the key is absent, but the key itself must always be present.","triggerScenarios":"Calling context.getGlobal() with zero arguments or null first argument in server markup JavaScript — usually an undefined key variable, or assuming getGlobal() with no args returns all globals.","commonSituations":"Reading cross-request state in mock-server handlers; retrieving a key whose name is computed from request parameters that are missing; confusing getGlobal() with a bulk getter.","solutions":["Pass the key explicitly: context.getGlobal('key').","Supply a default as the second argument instead of omitting the key: getGlobal('key', fallback).","Guard the key variable before the call.","If you need all globals, there is no such API — track keys separately in a global object."],"exampleFix":"// before\nvar v = context.getGlobal(key); // key undefined\n// after\nvar v = context.getGlobal(key || 'defaultKey', null);","handlingStrategy":"validation","validationCode":"// JS in markup\nif (typeof key !== 'string' || !key) key = 'defaultKey';","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the key; use the second argument for a default value","Don't treat getGlobal() as a bulk getter","Guard keys derived from request parameters","Centralize global key names as constants"],"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"}