{"record":{"id":"33b16537e91c01cb","repo":"karatelabs/karate","slug":"write-needs-value-and-path-arguments","errorCode":null,"errorMessage":"write() needs value and path arguments","messagePattern":"write\\(\\) needs value and path arguments","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":1145,"sourceCode":"            }\n            String path = args[0] + \"\";\n            Resource resource = getCurrentResource().resolve(path);\n            if (resource.isFile() && resource.getPath() != null) {\n                return resource.getPath().toAbsolutePath().toString();\n            }\n            // For classpath resources, return the prefixed path\n            return resource.getPrefixedPath();\n        };\n    }\n\n    /**\n     * karate.write(value, path) - Write content to a file.\n     * Path is relative to the output directory (e.g., target/karate-reports).\n     */\n    private JavaInvokable write() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"write() needs value and path arguments\");\n            }\n            Object value = args[0];\n            String path = args[1] + \"\";\n\n            // Get output directory\n            String outputDir = getOutputDir();\n\n            // Create the full path\n            File file = new File(outputDir, path);\n\n            // Ensure parent directories exist\n            File parent = file.getParentFile();\n            if (parent != null && !parent.exists()) {\n                parent.mkdirs();\n            }\n\n            // Convert value to bytes\n            byte[] bytes = KarateJsUtils.convertToBytes(value);","sourceCodeStart":1127,"sourceCodeEnd":1163,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L1127-L1163","documentation":"karate.write(value, path) writes content to a file under the output directory (e.g. target/karate-reports). It requires exactly two arguments — the value to write and the target path — and throws when fewer than two are supplied.","triggerScenarios":"`karate.write(value)` missing the path, `karate.write()` with nothing, or `karate.write(null)` — all have args.length < 2.","commonSituations":"Forgetting the path because a default was assumed; an optional path variable that was undefined leaving only one effective argument; translating karate.write(filePath, value) with swapped/merged arguments from older API memory.","solutions":["Supply both arguments: karate.write(content, 'output/result.txt')","Ensure the path variable is defined and a string before the call","Remember the path is relative to the output dir, not the classpath — pass a simple relative filename."],"exampleFix":"// before\nkarate.write(JSON.stringify(payload));\n// after\nkarate.write(JSON.stringify(payload), 'payload.json');","handlingStrategy":"validation","validationCode":"// JS\nif (value == null || path == null) karate.fail('karate.write needs value and path');\nkarate.write(value, path);","typeGuard":"function canWrite(v, p) { return arguments.length === 2 && p != null; }","tryCatchPattern":"try { karate.write(value, path); } catch (e) { karate.warn('write failed: ' + e); }","preventionTips":["Always supply both arguments to karate.write()","Resolve optional path variables with defaults: path = path || 'out.txt'","Note the path is relative to the output directory, not the classpath"],"tags":["javascript","argument-validation","file-io"],"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-19T12:17:13.211Z"}