karatelabs/karate · error · RuntimeException

prettyXml() needs one argument

Error message

prettyXml() needs one argument

What it means

prettyXml() is a Karate JS utility that pretty-prints XML, accepting either an XML Node or an XML string (parsed via Xml.toXmlDoc). It throws this error when called with zero arguments, since there is no XML to format.

Solutions

  1. Pass the XML value: prettyXml(xmlNode) or prettyXml(xmlString).
  2. Confirm the response variable actually holds the XML (e.g. response when the API returns XML, not a JSON-shaped field).
  3. If the value might be absent, print a fallback: prettyXml(xml || '<empty/>').

Example fix

// before
print prettyXml()
// after
print prettyXml(response)
Defensive patterns

Strategy: type-guard

Validate before calling

if (responseXml) { print prettyXml(responseXml) } else { print '(no xml)' }

Prevention

When it happens

Trigger: karate.prettyXml() with no arguments, e.g. prettyXml() instead of prettyXml(responseXml).

Common situations: Logging an XML response in a Scenario without passing it; the variable was renamed or removed; copying a debug snippet with a placeholder argument.

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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:436

            int step = args.length > 2 ? ((Number) args[2]).intValue() : 1;
            List<Integer> result = new ArrayList<>();
            if (step > 0) {
                for (int i = start; i < end; i += step) {
                    result.add(i);
                }
            } else if (step < 0) {
                for (int i = start; i > end; i += step) {
                    result.add(i);
                }
            }
            return result;
        };
    }

    static JavaInvokable prettyXml() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("prettyXml() needs one argument");
            }
            Object obj = args[0];
            if (obj instanceof Node) {
                return Xml.toString((Node) obj, true);
            } else if (obj instanceof String) {
                return Xml.toString(Xml.toXmlDoc((String) obj), true);
            } else {
                throw new RuntimeException("prettyXml() argument must be XML node or string");
            }
        };
    }

    static JavaInvokable repeat() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("repeat() needs two arguments: count and function");
            }
            int count = ((Number) args[0]).intValue();

View on GitHub (pinned to a22eb90246)