karatelabs/karate · error · JsErrorException
Cannot convert object to a BigInt
Error message
Cannot convert object to a BigInt
What it means
BigInt() called with an object (array, plain object, Date, Symbol, etc.) that has no valid primitive-of-BigInt conversion throws this TypeError, matching ECMAScript behavior of rejecting non-primitive inputs. Note null/undefined get a more specific message; everything else object-like lands here.
Solutions
- Extract the primitive field first: BigInt(obj.value)
- Use valueOf()/toString() explicitly on wrapper objects before converting
- Log/inspect the value's type to find the wrong nesting level
- Add a scalar check: if (typeof v !== 'number' && typeof v !== 'string' && typeof v !== 'bigint') throw new TypeError('expected scalar')
Example fix
// before let bi = BigInt(payload.ids); // array -> TypeError // after let bi = BigInt(payload.ids[0]);
Defensive patterns
Strategy: type-guard
Validate before calling
function toBigIntSafe(x) {
if (x == null) return null;
if (typeof x === 'bigint') return x;
if (typeof x === 'number' && Number.isInteger(x)) return BigInt(x);
if (typeof x === 'string' && /^-?\d+$/.test(x.trim())) return BigInt(x.trim());
return null; // objects, arrays, symbols
} Type guard
const isScalar = (x) => ['bigint','number','string'].includes(typeof x);
Try / catch
let bi;
try { bi = BigInt(x); } catch (e) {
if (e instanceof TypeError && /object/.test(e.message)) bi = BigInt(x.value ?? 0);
else throw e;
} Prevention
- Extract scalar fields before converting wrapper objects/arrays
- typeof-check inputs at module boundaries (JSON parses, host objects)
- Avoid passing parsed JSON sub-trees directly into BigInt()
- Add schema validation for API payloads with BigInt-bound fields
When it happens
Trigger: BigInt({}), BigInt([5]), BigInt(new Date()), BigInt(Symbol('x')), BigInt(someProxyOrClassInstance) — any value failing the Number/String/null/undefined checks in primitiveToBigInt.
Common situations: Passing parsed JSON sub-objects instead of scalar fields; forgetting to index into an array (BigInt(rows) vs BigInt(rows[0].id)); wrapping values in Java host objects in Karate scripts.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot convert undefined to a BigInt
- Cannot convert a BigInt to a number
- BigInt.prototype method called on non-BigInt
- Cannot mix BigInt and other types, use explicit conversions
- BigInts have no unsigned right shift, use >> instead
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/85aad45bc21898d4.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java:121
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);
}
return new BigInteger(trimmed);
} catch (NumberFormatException e) {
throw JsErrorException.syntaxError("Cannot convert " + s + " to a BigInt");
}
}
if (value == null || value == Terms.UNDEFINED) {
throw JsErrorException.typeError("Cannot convert " + (value == null ? "null" : "undefined") + " to a BigInt");
}
throw JsErrorException.typeError("Cannot convert object to a BigInt");
}
private Object asIntN(Context context, Object[] args) {
int bits = toIndex(args.length > 0 ? args[0] : Terms.UNDEFINED, (CoreContext) context);
BigInteger bi = toBigInt(args.length > 1 ? args[1] : Terms.UNDEFINED, (CoreContext) context);
if (bits == 0) return BigInteger.ZERO;
// mod 2^bits, then signed reinterpret
BigInteger mod = BigInteger.ONE.shiftLeft(bits);
BigInteger r = bi.mod(mod);
BigInteger half = BigInteger.ONE.shiftLeft(bits - 1);
if (r.compareTo(half) >= 0) {
r = r.subtract(mod);
}
return r;
}
private Object asUintN(Context context, Object[] args) {
int bits = toIndex(args.length > 0 ? args[0] : Terms.UNDEFINED, (CoreContext) context);View on GitHub (pinned to a22eb90246)