karatelabs/karate · error · ParserException
invalid binary literal
Error message
invalid binary literal
What it means
Karate's embedded JS lexer throws this when it sees a `0b` prefix but no binary digit (0 or 1) follows. The lexer requires at least one valid binary digit after the `0b`/`0B` marker, matching the ECMAScript numeric literal grammar. It is a ParserException raised during tokenization, before parsing.
Solutions
- Add at least one binary digit after the `0b` prefix (e.g. `0b1010` instead of `0b`).
- Remove the `0b` prefix if a decimal value was intended (e.g. `0b` -> `0`).
- Check the character immediately after `0b` — only `0` and `1` are valid; `2`-`9` or `_` start the literal incorrectly.
- Escape or move the text out of JS context if `0b` is not meant to be a literal (e.g. inside a string).
Example fix
// before * def x = 0b // after * def x = 0b1010
Defensive patterns
Strategy: validation
Validate before calling
// validate before passing to karate eval
function isValidJsNumberLiteral(s) {
return /^0[bB][01]+n?$/.test(s.trim());
}
if (!isValidJsNumberLiteral(expr)) throw new Error('bad binary literal: ' + expr); Try / catch
try {
def x = eval(expr);
} catch (e) {
if (e.message && e.message.indexOf('invalid binary literal') >= 0) {
karate.log('malformed binary literal, fix 0b usage: ' + expr);
}
throw e;
} Prevention
- Only use 0b prefix when you actually mean binary; else use decimal.
- Verify digits after 0b are only 0 or 1.
- Keep numeric constants in shared JS/feature files under review for typos.
- Escape 0b sequences that are textual data as strings.
When it happens
Trigger: Writing JS in a Karate script with a malformed binary literal such as `0b`, `0b2`, `0b_1`, or `0B` — the `0b` is immediately followed by a non-binary character or end of token.
Common situations: Typo in a bitwise operation or numeric constant inside embedded JS expressions (`karate.js`, `eval` blocks, `* assert` steps); hand-editing constants and dropping a digit; copy-pasted code where digits after `0b` were lost.
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
- invalid numeric separator
- invalid octal literal
- missing exponent digits
- invalid escape sequence in template literal: \
- invalid escape sequence in template literal: \0
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/bdabeb607a09cdb1.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/parser/JsLexer.java:480
}
// Rare path: BigInt suffix — `0xff_ffn` is valid
if (!isAtEnd() && peek() == 'n') {
advance();
return BIGINT;
}
return NUMBER;
}
// Binary number (0b / 0B)
if (c == '0' && (peek(1) == 'b' || peek(1) == 'B')) {
advance(); // 0
advance(); // b
int binStart = pos;
while (!isAtEnd() && (peek() == '0' || peek() == '1')) {
advance();
}
if (pos == binStart) {
throw new ParserException("invalid binary literal");
}
if (!isAtEnd() && peek() == '_') {
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();View on GitHub (pinned to a22eb90246)