{"record":{"id":"e5a4cf62c32b1c7a","repo":"karatelabs/karate","slug":"switch-requires-a-template-argument","errorCode":null,"errorMessage":"switch() requires a template argument","messagePattern":"switch\\(\\) requires a template argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java","lineNumber":367,"sourceCode":"                if (args.length == 0) throw new RuntimeException(\"redirect() requires a URL argument\");\n                redirect(args[0].toString());\n                return null;\n            };\n            case \"log\" -> (JavaInvokable) args -> {\n                log(args);\n                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;","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java#L349-L385","documentation":"The server context's switch() method switches server-side processing to another template and requires one template-name argument. Calling switch() with no arguments throws this RuntimeException before any template lookup happens.","triggerScenarios":"Calling context.switch() without arguments in server markup JavaScript — usually a template name variable that is undefined/empty at call time, or calling switch() for its side effects expecting it to be a no-op.","commonSituations":"Multi-template server flows (e.g. switching between login and error pages) where the next-template name is derived from request data; typos between switch() and switchTemplate; dynamic routing handlers in Karate mock servers.","solutions":["Pass the template name explicitly: context.switch('other-template').","Guard the template-name variable and fall back to a default template when missing.","Verify the expression computing the template name (e.g. request params) isn't yielding undefined.","If conditional rendering is the goal, use an if in markup instead of switch()."],"exampleFix":"// before\ncontext.switch(nextPage); // undefined\n// after\ncontext.switch(nextPage || 'default-template');","handlingStrategy":"validation","validationCode":"// JS in markup\nif (typeof tpl !== 'string' || !tpl) tpl = 'default-template';","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a literal or defaulted template name to context.switch()","Guard dynamic template names derived from request data","Use markup conditionals for optional rendering instead of switch()","Cover multi-template flows with server tests"],"tags":["server","javascript","missing-argument","templates"],"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"}