{"record":{"id":"0b0381a1d223d53a","repo":"karatelabs/karate","slug":"read-needs-at-least-one-argument","errorCode":null,"errorMessage":"read() needs at least one argument","messagePattern":"read\\(\\) needs at least one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":203,"sourceCode":"            Map<String, Object> vars;\n            if (args.length > 1) {\n                vars = (Map<String, Object>) args[1];\n            } else {\n                vars = null;\n            }\n            String html = markup().processPath(read, vars);\n            onDoc.accept(html);\n            return null;\n        };\n    }\n\n    // ========== Engine-Dependent Methods ==========\n    // These methods require access to the JavaScript engine for evaluation.\n\n    private JavaCallable initRead() {\n        return (context, args) -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"read() needs at least one argument\");\n            }\n            String rawPath = args[0] + \"\";\n\n            // Parse tag selector for feature files\n            // Supports: file.feature@tag or @tag (same-file)\n            String path;\n            String tagSelector = null;\n            if (rawPath.startsWith(\"@\")) {\n                // Same-file tag - return a FeatureCall wrapper\n                return new FeatureCall(null, rawPath);\n            } else {\n                int tagPos = rawPath.indexOf(\".feature@\");\n                if (tagPos != -1) {\n                    path = rawPath.substring(0, tagPos + 8);  // \"file.feature\"\n                    tagSelector = \"@\" + rawPath.substring(tagPos + 9);  // \"@tag\"\n                } else {\n                    path = rawPath;\n                }","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L185-L221","documentation":"karate.read() requires at least one argument: the path of the file (feature, json, xml, etc.) to load. Karate throws this immediately when the JS engine invokes the `read` callable with an empty argument list, since there is no meaningful default file to resolve. It is an argument-validation guard at the top of initRead().","triggerScenarios":"Calling karate.read() with zero arguments, e.g. read() or read(...apply() with an empty args array) in a JS block or karate.call expression where the path argument evaluates away.","commonSituations":"Interpolating the path from a variable that is undefined/empty so the call collapses to no argument; typos like karate.read; dynamically building read calls in JS where an array spread passes nothing.","solutions":["Pass the file path as the first argument, e.g. karate.read('classpath:my.feature').","Verify the variable holding the path is defined and non-empty before the call.","If using apply/spread, ensure the arguments array has at least one element.","Check interpolation syntax so the path expression does not silently evaluate to nothing."],"exampleFix":"// before\nvar text = karate.read();\n// after\nvar text = karate.read('classpath:data/payload.json');","handlingStrategy":"validation","validationCode":"if (!path) throw new Error('read() requires a file path');\nvar text = karate.read('classpath:' + path);","typeGuard":"function hasReadArg(args) { return Array.isArray(args) && args.length > 0 && args[0] != null && args[0] !== ''; }","tryCatchPattern":"var text;\ntry { text = karate.read(path); }\ncatch (e) { if ((e.message || '').indexOf('read() needs at least one argument') >= 0) karate.fail('read() called without a path'); throw e; }","preventionTips":["Always pass a string literal or verified variable as the first argument to read().","Log/defend interpolated path variables before use.","Prefer classpath: prefixed paths to avoid resolution ambiguity."],"tags":["javascript","argument-validation","karate"],"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"}