karatelabs/karate · error · RuntimeException
Failed to read text from
Error message
Failed to read text from: {path} What it means
PathResource.getText() lazily reads the file's bytes (Files.readAllBytes) and decodes them to a String, caching the result. Any I/O failure during the read — file missing, unreadable, is a directory, or too large to hold in memory — is wrapped in this RuntimeException naming the path.
Solutions
- Confirm the path in the message exists and is a readable file before calling getText
- Run from the expected working directory or use absolute/classpath paths for fixtures
- Check file permissions for the executing user
- For large or binary files, stream via getStream() instead of loading all text
Example fix
// before
String text = Resource.path("config/missing.json").getText();
// after
Resource r = Resource.path("classpath:config/app.json");
if (r.exists()) {
String text = r.getText();
} Defensive patterns
Strategy: validation
Validate before calling
// Java: pre-check existence and readability before getText
Path p = Paths.get("config/app.json");
if (!Files.isRegularFile(p) || !Files.isReadable(p)) {
throw new IllegalStateException("fixture missing or unreadable: " + p);
} Try / catch
try {
String text = pathResource.getText();
} catch (RuntimeException e) {
if (e.getMessage().startsWith("Failed to read text from:")) {
logger.error("read failed for {} cause: {}", e.getMessage(), e.getCause());
}
} Prevention
- Use classpath: prefixes for fixtures to be independent of the working directory
- Call exists() before getText when the file may not have been produced yet
- Stream large/binary files with getStream() instead of reading all text
- Keep fixtures out of clean-plugin targets in CI
When it happens
Trigger: Reading a resource whose file was deleted or moved after PathResource creation; no read permission; path refers to a directory; attempting to read a huge binary as text causing OOM wrapped as a read error; file on an unavailable network drive.
Common situations: Tests reading config/fixtures from a relative path that differs between local and CI working directories; fixtures removed by a clean step running concurrently; expecting text from a directory path by mistake.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- Failed to read bytes from
- failed to read item at index
- failed to read next line
- Failed to materialize resource to
- Failed to open stream for
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/7d72aa4503ac810c.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/common/PathResource.java:225
return Files.newInputStream(path);
} catch (Exception e) {
throw new RuntimeException("Failed to open stream for: " + path, e);
}
}
@Override
public String getRelativePath() {
return relativePath;
}
@Override
public String getText() {
if (text == null) {
try {
bytes = Files.readAllBytes(path);
text = FileUtils.toString(bytes);
} catch (Exception e) {
throw new RuntimeException("Failed to read text from: " + path, e);
}
}
return text;
}
@Override
public String getLine(int index) {
if (lines == null) {
lines = getText().split("\\r?\\n");
}
return lines[index];
}
@Override
public long getLastModified() {
try {
return Files.getLastModifiedTime(path).toMillis();
} catch (Exception e) {View on GitHub (pinned to a22eb90246)