karatelabs/karate · error · JsErrorException
Cannot convert non-finite number to BigInt
Error message
Cannot convert non-finite number to BigInt
What it means
Spec ToBigInt rejects numbers that are not finite integers. Karate's primitiveToBigInt throws RangeError 'Cannot convert non-finite number to BigInt' for Infinity, -Infinity, and NaN, and a sibling RangeError for non-integer finite numbers.
Solutions
- Check Number.isFinite(v) before BigInt(v)
- Round or validate integrality: Number.isInteger(v) || Math.round(v)
- Fix upstream math that produced Infinity/NaN (division by zero, failed parse)
- Parse strings directly: BigInt('123') instead of going through float
Example fix
// before
const n = BigInt(total / count); // Infinity when count === 0
// after
if (!Number.isFinite(total / count) || !Number.isInteger(total / count)) throw new Error('bad input');
const n = BigInt(Math.trunc(total / count)); Defensive patterns
Strategy: validation
Validate before calling
if (typeof v === 'number' && (!Number.isFinite(v) || !Number.isInteger(v))) throw new Error('BigInt needs a finite integer, got ' + v); Type guard
function isSafeBigIntSource(x) { return typeof x === 'bigint' || (typeof x === 'number' && Number.isFinite(x) && Number.isInteger(x)) || (typeof x === 'string' && /^-?\d+$/.test(x)); } Try / catch
try { return BigInt(v); } catch (e) { if (String(e.message).includes('non-finite') || String(e.message).includes('non-integer')) return 0n; throw e; } Prevention
- Guard divisions that can yield Infinity/NaN before BigInt conversion
- Parse integer strings directly with BigInt(str) instead of via float
- Round or reject fractional values explicitly
When it happens
Trigger: BigInt(Infinity); BigInt(-Infinity); BigInt(NaN); BigInt(1.5) (non-integer variant); dividing by zero or parsing failing before a BigInt conversion.
Common situations: Division results that became Infinity (x/0 in JS), parseFloat on malformed strings yielding NaN, averaging routines producing non-integers, JSON payloads with non-numeric sentinels.
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
- Cannot convert non-integer number to BigInt
- ToIndex: value must be non-negative
- toString() radix must be between 2 and 36
- Cannot convert a BigInt to a number using unary +
- array index too large for dense storage:
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/087866a4a3568523.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java:94
// No-context overload for code paths that have only a primitive in hand
// (e.g. asIntN/asUintN second arg already coerced). Matches spec when the
// input is guaranteed primitive.
static BigInteger toBigInt(Object value) {
return toBigInt(value, null);
}
private static BigInteger primitiveToBigInt(Object value) {
if (value instanceof BigInteger bi) {
return bi;
}
if (value instanceof Boolean b) {
return b ? BigInteger.ONE : BigInteger.ZERO;
}
if (value instanceof Number n) {
double d = n.doubleValue();
if (!Double.isFinite(d)) {
throw JsErrorException.rangeError("Cannot convert non-finite number to BigInt");
}
if (d != Math.floor(d)) {
throw JsErrorException.rangeError("Cannot convert non-integer number to BigInt");
}
return new BigDecimal(d).toBigInteger();
}
if (value instanceof String s) {
String trimmed = s.trim();
if (trimmed.isEmpty()) {
return BigInteger.ZERO;
}
try {
if (trimmed.length() > 2 && trimmed.charAt(0) == '0') {
char c = trimmed.charAt(1);
if (c == 'x' || c == 'X') return new BigInteger(trimmed.substring(2), 16);
if (c == 'o' || c == 'O') return new BigInteger(trimmed.substring(2), 8);
if (c == 'b' || c == 'B') return new BigInteger(trimmed.substring(2), 2);
}View on GitHub (pinned to a22eb90246)