karatelabs/karate · error · RuntimeException

failed to read next line

Error message

failed to read next line

What it means

DiskBackedIterator.hasNext() wraps IOException from reading the next line of the backing file in a RuntimeException with this message, after closing the reader. It signals the sequential read over the temp file failed mid-iteration.

Solutions

  1. Inspect the chained IOException cause
  2. Iterate soon after creation and complete the iteration without long pauses
  3. Keep the temp file on stable storage and protected from external cleanup
  4. Catch RuntimeException around iteration if partial-failure recovery is needed

Example fix

// before
while (it.hasNext()) { process(it.next()); } // no failure handling
// after
try {
    while (it.hasNext()) { process(it.next()); }
} catch (RuntimeException e) {
    log.error("iteration failed", e.getCause());
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (backingFileOf(list) == null || !backingFileOf(list).exists()) throw new IOException("backing file missing before iteration");

Type guard

boolean canReadBacking(DiskBackedList l) { try { var it = l.iterator(); it.hasNext(); return true; } catch (RuntimeException e) { return false; } }

Try / catch

try { while (it.hasNext()) process(it.next()); } catch (RuntimeException e) { if ("failed to read next line".equals(e.getMessage())) { log.error("disk read failed mid-iteration", e.getCause()); } throw e; }

Prevention

When it happens

Trigger: Disk I/O error or file deleted/truncated while the iterator is mid-read; descriptor closed unexpectedly; filesystem became unavailable during iteration.

Common situations: Long iterations over big lists on unstable storage; tmp reapers deleting the file partway through; resource exhaustion on the host.

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/6cf39b500f543ac1. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/match/DiskBackedList.java:260

        public boolean hasNext() {
            if (hasNextCalled) {
                return nextLine != null;
            }
            hasNextCalled = true;
            if (currentIndex >= size) {
                closeReader();
                return false;
            }
            try {
                nextLine = reader.readLine();
                if (nextLine == null) {
                    closeReader();
                    return false;
                }
                return true;
            } catch (IOException e) {
                closeReader();
                throw new RuntimeException("failed to read next line", e);
            }
        }

        @Override
        public Object next() {
            if (!hasNextCalled) {
                hasNext();
            }
            if (nextLine == null) {
                throw new NoSuchElementException();
            }
            hasNextCalled = false;
            currentIndex++;
            return deserializeItem(nextLine);
        }

        private void closeReader() {
            try {

View on GitHub (pinned to a22eb90246)