karatelabs/karate · error · RuntimeException

readBytes() requires a path argument

Error message

readBytes() requires a path argument

What it means

MarkupContext exposes a JS-callable `readBytes(path)` function that returns the raw bytes of a resource at the given path. When invoked with no arguments or a null first argument, the JavaInvokable wrapper throws this RuntimeException, since there is no path to read.

Solutions

  1. Pass a non-null path string: `context.readBytes('/assets/logo.png')`.
  2. Guard dynamic paths before calling: `path != null ? context.readBytes(path) : fallbackBytes`.
  3. Verify the model variable feeding the path is populated in the render context.
  4. Distinguish 'no argument' from 'file not found' — this error is about the missing argument; a valid path that doesn't exist fails later in readBytes with a different error.

Example fix

// before
var bytes = context.readBytes(assetPath);

// after
var bytes = assetPath != null ? context.readBytes(assetPath) : null;
Defensive patterns

Strategy: validation

Validate before calling

function safeReadBytes(ctx, path) {
  if (path == null || path === '') return null;
  return ctx.readBytes(path);
}

Type guard

function hasPath(a) { return a != null && typeof a === 'string' && a.length > 0; }
// usage: if (hasPath(p)) context.readBytes(p);

Try / catch

try {
  var bytes = context.readBytes(p);
} catch (e) {
  if (e.message === 'readBytes() requires a path argument') {
    bytes = null;
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling `context.readBytes()` or `context.readBytes(null)` in template script — argument omitted or bound to a null model value.

Common situations: Dynamic byte-asset paths (images, files) that resolve to null when the model key is absent; missing argument after refactor; copying the `read()` call pattern and leaving the argument empty.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/34f17d72a8e88f9a. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java:133

    /**
     * Default implementation of jsGet that exposes context methods to JavaScript.
     * Implementations can override to add more properties/methods.
     */
    @Override
    default Object jsGet(String key) {
        return switch (key) {
            case "template" -> getTemplateName();
            case "caller" -> getCallerTemplateName();
            case "read" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("read() requires a path argument");
                }
                return read(args[0].toString());
            };
            case "readBytes" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("readBytes() requires a path argument");
                }
                return readBytes(args[0].toString());
            };
            case "toJson" -> (JavaInvokable) args -> {
                // null IS a valid JSON-able value (`null`), so only the
                // missing-arg case errors here.
                if (args.length == 0) throw new RuntimeException("toJson() requires an object argument");
                return toJson(args[0]);
            };
            case "fromJson" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("fromJson() requires a JSON string argument");
                }
                return fromJson(args[0].toString());
            };
            // context.get(name, default?) for optional fragment params.
            // Walks the current eval scope (`_` map first, then wrapped
            // Thymeleaf scope). Returns the bound non-null value if found,

View on GitHub (pinned to a22eb90246)