karatelabs/karate · error · io.karatelabs.parser.ParserException

too much recursion

Error message

too much recursion

What it means

enter(NodeType) pushes a new parse-frame onto a fixed-depth stack (MAX_DEPTH = 128). When the parser's recursive-descent nesting exceeds that limit it throws ParserException("too much recursion") instead of overflowing the JVM stack, protecting against pathological nesting in the parsed source.

Solutions

  1. Reduce nesting: split the expression into intermediate variables or multiple steps.
  2. Pretty-print/reformat minified code — nesting depth stays the same, so refactor deeply nested literals into sequential statements.
  3. If the input is legitimately deep, load it as data (JSON file) rather than embedding it as a JS literal.

Example fix

// before
var x = [[[[[ ... 200 levels ... ]]]]];
// after
var part1 = [[ ... ]];
var part2 = [[ ... ]];
var x = part1.concat(part2);
Defensive patterns

Strategy: validation

Validate before calling

// Bound nesting depth before eval (simple heuristic)
int depth = 0, max = 0;
for (char c : script.toCharArray()) {
    if (c=='('||c=='['||c=='{') depth = ++max > depth ? max : depth;
    if (c==')'||c==']'||c=='}') depth--;
}
if (max > 100) throw new IllegalArgumentException("script nesting too deep for parser (max 128)");

Try / catch

try {
    return karate.eval(script);
} catch (ParserException e) {
    if ("too much recursion".equals(e.getMessage())) {
        throw new IllegalArgumentException("script exceeds parser nesting depth (128); flatten the expression", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Parsing source with deeply nested expressions/structures — e.g. hundreds of nested parentheses, a huge chained expression, machine-generated or minified JSON-in-JS with extreme nesting.

Common situations: Embedding very large minified JS payloads or auto-generated scripts in a Karate feature; deeply nested array/object literals in inline scripts.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/parser/BaseParser.java:211

            return true;
        }
        error(token);
        return errorRecoveryEnabled; // Continue if recovering, never reached otherwise
    }

    /**
     * Exit that tolerates incomplete nodes.
     * Even if the node is incomplete, it is added to the parent.
     */
    protected boolean exitSoft() {
        return exit(true, false, Shift.NONE);
    }

    // ========== End Error Recovery Methods ==========

    protected void enter(NodeType type) {
        if (stackPointer >= MAX_DEPTH) {
            throw new ParserException("too much recursion");
        }
        positionStack[stackPointer] = position;
        nodeStack[stackPointer] = new Node(type);
        stackPointer++;
    }

    // Single-token overload - avoids array allocation
    protected boolean enter(NodeType type, TokenType token) {
        if (peek() != token) {
            return false;
        }
        if (stackPointer >= MAX_DEPTH) {
            throw new ParserException("too much recursion");
        }
        positionStack[stackPointer] = position;
        nodeStack[stackPointer] = new Node(type);
        stackPointer++;
        consumeNext();

View on GitHub (pinned to a22eb90246)