{"record":{"id":"d5aa3f7e1323cb02","repo":"karatelabs/karate","slug":"read-requires-a-path-argument","errorCode":null,"errorMessage":"read() requires a path argument","messagePattern":"read\\(\\) requires a path argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java","lineNumber":127,"sourceCode":"     * Default returns null; implementations override to surface the scope\n     * to the {@code context.get} dispatch path.\n     */\n    default MarkupScope getMarkupScope() {\n        return null;\n    }\n\n    /**\n     * Default implementation of jsGet that exposes context methods to JavaScript.\n     * Implementations can override to add more properties/methods.\n     */\n    @Override\n    default Object jsGet(String key) {\n        return switch (key) {\n            case \"template\" -> getTemplateName();\n            case \"caller\" -> getCallerTemplateName();\n            case \"read\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"read() requires a path argument\");\n                }\n                return read(args[0].toString());\n            };\n            case \"readBytes\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"readBytes() requires a path argument\");\n                }\n                return readBytes(args[0].toString());\n            };\n            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\");","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java#L109-L145","documentation":"MarkupContext exposes a JS-callable `read(path)` function to load template/resource content by path. When it is invoked with no arguments or with null as the first argument, the JavaInvokable wrapper throws this RuntimeException because there is no path to resolve.","triggerScenarios":"Calling `context.read()` or `context.read(null)` inside a template script/expression where the path argument is missing or evaluates to null.","commonSituations":"A template variable holding the path is undefined/null at render time (e.g. `context.read(someVar)` with an unset model key); forgetting the argument when converting an inline include to `read()`; conditional template paths that resolve to null.","solutions":["Pass an explicit path string: `context.read('/fragments/header.html')`.","If the path comes from a variable, guard it: only call read when the variable is present, or supply a default (`someVar ?: '/fragments/header.html'`).","Check the model/scope for the key feeding the path — a missing model value is usually the root cause.","If the intent was optional inclusion, wrap the call in a condition rather than passing null."],"exampleFix":"// before\nvar content = context.read(pathOrNull);\n\n// after\nvar content = pathOrNull != null ? context.read(pathOrNull) : '';","handlingStrategy":"validation","validationCode":"function safeRead(ctx, path) {\n  if (path == null || path === '') return null;\n  return ctx.read(path);\n}","typeGuard":"function hasPath(a) { return a != null && typeof a === 'string' && a.length > 0; }\n// usage: if (hasPath(p)) context.read(p);","tryCatchPattern":"try {\n  var content = context.read(p);\n} catch (e) {\n  if (e.message === 'read() requires a path argument') {\n    content = '';\n  } else { throw e; }\n}","preventionTips":["Never call context.read() with zero or null arguments","Default dynamic path variables before use (`p ?: '/fragments/default.html'`)","Verify model keys feeding paths are populated","Distinguish missing-argument errors from file-not-found errors in handlers"],"tags":["markup","javascript-binding","missing-argument","template"],"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"}