{"record":{"id":"b7a4dc5cfa473232","repo":"karatelabs/karate","slug":"failed-to-read-file","errorCode":null,"errorMessage":"failed to read file: ","messagePattern":"failed to read file: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/StepExecutor.java","lineNumber":2445,"sourceCode":"        Map<String, Object> multipartMap = new HashMap<>();\n        multipartMap.put(\"name\", name);\n\n        if (value instanceof Map) {\n            Map<String, Object> fileMap = (Map<String, Object>) value;\n            // Handle { read: 'path', filename: 'name', contentType: 'type' }\n            Object readPath = fileMap.get(\"read\");\n            if (readPath != null) {\n                Resource resource = resolveResource(readPath.toString());\n                File file = getFileFromResource(resource);\n                if (file != null) {\n                    multipartMap.put(\"value\", file);\n                } else {\n                    // For classpath or in-memory resources, read bytes\n                    try (InputStream is = resource.getStream()) {\n                        byte[] bytes = is.readAllBytes();\n                        multipartMap.put(\"value\", bytes);\n                    } catch (Exception e) {\n                        throw new RuntimeException(\"failed to read file: \" + readPath, e);\n                    }\n                }\n            } else if (fileMap.get(\"value\") != null) {\n                multipartMap.put(\"value\", fileMap.get(\"value\"));\n            }\n            // Copy other properties\n            if (fileMap.get(\"filename\") != null) {\n                multipartMap.put(\"filename\", fileMap.get(\"filename\"));\n            }\n            if (fileMap.get(\"contentType\") != null) {\n                multipartMap.put(\"contentType\", fileMap.get(\"contentType\"));\n            }\n            if (fileMap.get(\"charset\") != null) {\n                multipartMap.put(\"charset\", fileMap.get(\"charset\"));\n            }\n            if (fileMap.get(\"transferEncoding\") != null) {\n                multipartMap.put(\"transferEncoding\", fileMap.get(\"transferEncoding\"));\n            }","sourceCodeStart":2427,"sourceCodeEnd":2463,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/StepExecutor.java#L2427-L2463","documentation":"When building a `multipart file` part whose value comes from a `read:` path, Karate resolves the resource (classpath or filesystem) and reads its bytes. If reading the resolved resource's stream fails, it wraps the exception in this RuntimeException naming the read path.","triggerScenarios":"`multipart file x = { read: 'some/path' }` where the resource exists enough to resolve a stream but reading fails — e.g. unreadable file permissions, directory instead of file, I/O error, or broken classpath resource.","commonSituations":"File exists at build time but not packaged into the test classpath/jar; path points to a directory; permissions changed; file deleted between resolution and read in CI.","solutions":["Verify the read path resolves to a readable regular file; check permissions with `ls -l`.","Ensure the file is on the classpath (or use an absolute/relative filesystem path) and included in the build artifact.","If the path may not exist, pre-check with Java's Files.exists / karate's file utilities before the multipart step.","Wrap scenario setup so a missing fixture fails fast with a clear message."],"exampleFix":"// before\nAnd multipart file upload = { read: 'classpath:missing.txt' }\n// after (verify fixture exists first)\n* def text = karate.read('classpath:data/file.txt')\nAnd multipart file upload = { read: 'classpath:data/file.txt', filename: 'file.txt' }","handlingStrategy":"validation","validationCode":"// Pre-check the resource resolves and is readable\n* def path = 'classpath:data/file.txt'\n* def exists = karate.read(path) != null\n* assert exists","typeGuard":null,"tryCatchPattern":"try {\n    // multipart file step\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"failed to read file:\")) {\n        karate.log('Check fixture path/permissions: ' + e.getCause());\n    }\n    throw e;\n}","preventionTips":["Keep fixtures under src/test/resources so they land on the classpath.","Verify file permissions, especially in CI containers.","Pre-read files with karate.read to fail fast with clearer errors.","Pass in-memory bytes via `value` when the file was already loaded."],"tags":["karate","multipart","file-io","classpath"],"backgroundTag":"file-read-failed","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"}