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

  1. Confirm the path in the message exists and is a readable file before calling getText
  2. Run from the expected working directory or use absolute/classpath paths for fixtures
  3. Check file permissions for the executing user
  4. 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

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


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)