karatelabs/karate · error · ParserException

invalid octal literal

Error message

invalid octal literal

What it means

The lexer throws this when an `0o`/`0O` octal prefix is not followed by any octal digit (0-7). ES2015 octal literals require at least one digit after the prefix. This is a tokenization-time ParserException.

Solutions

  1. Provide a valid octal digit sequence after `0o` (e.g. `0o755`).
  2. Use decimal instead if the value is 8 or 9 after the prefix (`0o8` is invalid — use `8`).
  3. Verify every digit after `0o` is in 0-7.
  4. If not meant as a number, quote it as a string.

Example fix

// before
* def mode = 0o8
// after
* def mode = 0o10
Defensive patterns

Strategy: validation

Validate before calling

function isValidOctalLiteral(s) {
  return /^0[oO][0-7]+n?$/.test(s.trim());
}
if (!isValidOctalLiteral(expr)) throw new Error('bad octal literal: ' + expr);

Try / catch

try {
  def x = eval(expr);
} catch (e) {
  if (e.message && e.message.indexOf('invalid octal literal') >= 0) {
    karate.log('malformed octal literal: ' + expr);
  }
  throw e;
}

Prevention

When it happens

Trigger: A JS expression contains `0o`, `0O`, `0o8`, `0o9`, or `0o_1` — the prefix is followed by a character outside 0-7 or by end of token.

Common situations: Porting old C-style octal code (`0755`) into ES-style octal incorrectly (`0o755` typo as `0o759`); permission-constant typos; dropped digits after manual edits.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/parser/JsLexer.java:501

                scanBinaryDigitsWithSeparators();
            }
            if (!isAtEnd() && peek() == 'n') {
                advance();
                return BIGINT;
            }
            return NUMBER;
        }

        // Octal number (0o / 0O)
        if (c == '0' && (peek(1) == 'o' || peek(1) == 'O')) {
            advance(); // 0
            advance(); // o
            int octStart = pos;
            while (!isAtEnd() && peek() >= '0' && peek() <= '7') {
                advance();
            }
            if (pos == octStart) {
                throw new ParserException("invalid octal literal");
            }
            if (!isAtEnd() && peek() == '_') {
                scanOctalDigitsWithSeparators();
            }
            if (!isAtEnd() && peek() == 'n') {
                advance();
                return BIGINT;
            }
            return NUMBER;
        }

        boolean isInteger = true;

        // Decimal number
        // Integer part
        if (c == '.') {
            // Number starting with .
            isInteger = false;

View on GitHub (pinned to a22eb90246)