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

parser state: [ ]

Error message

<message>
<position> <token>
parser state: <state>[<hint>]

What it means

BaseParser.error() raises a ParserException carrying the failure message, the offending token with its position, the parser state (a window of tokens plus the current node-stack path), and optionally an ASI (automatic semicolon insertion) hint. It is the parser's central syntax-error thrower, used by consumeSoft, exit, and consume when an expected token or structure is missing.

Solutions

  1. Read the message: it names the expected construct, shows the offending token with file:line:col, and prints parser state.
  2. Apply the ASI hint if present (usually 'add a semicolon here').
  3. Inspect the indicated position and fix the syntax (balance brackets, complete the expression).

Example fix

// before (script)
var x = { a: 1, b: 2 // missing }
// after
var x = { a: 1, b: 2 };
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-validate scripts when possible: run through the parser in error-recovery mode
List<SyntaxError> problems = parser.tryParse(source); // collect before executing

Try / catch

try {
    result = karate.eval(script);
} catch (ParserException e) {
    // message contains file:line:col, token, parser state, and ASI hint
    logger.error("syntax error:\n{}", e.getMessage());
    throw new IllegalArgumentException("invalid script at " + extractLocation(e.getMessage()), e);
}

Prevention

When it happens

Trigger: Any syntactically invalid Karate/JS script: a missing closing bracket/paren, a keyword used where an expression is expected, an unterminated construct, a missing semicolon that ASI cannot fix.

Common situations: Copy-pasted JS with platform-specific syntax, missing braces in feature-file JS blocks, stray characters, editing scripts by hand and dropping a paren or comma.

Related errors


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

Appendix: source

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

        }
        if (stackPointer >= 2) {
            sb.append(nodeStack[stackPointer - 2].type).append(" >> ");
        }
        sb.append('[').append(nodeStack[stackPointer - 1].type).append(']');
        return sb.toString();
    }

    protected void error(String message) {
        Token token = peekToken();
        if (errorRecoveryEnabled) {
            errors.add(new SyntaxError(token, message));
            return;
        }
        String hint = AsiHint.forFailure(tokens, position);
        if (token.getResource().isFile()) {
            System.err.println("file://" + token.getResource().getUri().getPath() + ":" + token.getPositionDisplay() + " " + message);
        }
        throw new ParserException(message + "\n"
                + token.getPositionDisplay()
                + " " + token + "\nparser state: " + this
                + (hint == null ? "" : "\n" + hint));
    }

    protected void error(NodeType... expected) {
        if (errorRecoveryEnabled) {
            errors.add(new SyntaxError(peekToken(), "expected: " + Arrays.asList(expected), expected[0]));
            return;
        }
        error("expected: " + Arrays.asList(expected));
    }

    protected void error(TokenType... expected) {
        if (errorRecoveryEnabled) {
            errors.add(new SyntaxError(peekToken(), "expected: " + Arrays.asList(expected)));
            return;
        }

View on GitHub (pinned to a22eb90246)