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

  1. Extract the primitive field first: BigInt(obj.value)
  2. Use valueOf()/toString() explicitly on wrapper objects before converting
  3. Log/inspect the value's type to find the wrong nesting level
  4. 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

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


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)