{"record":{"id":"85aad45bc21898d4","repo":"karatelabs/karate","slug":"cannot-convert-object-to-a-bigint","errorCode":null,"errorMessage":"Cannot convert object to a BigInt","messagePattern":"Cannot convert object to a BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java","lineNumber":121,"sourceCode":"            if (trimmed.isEmpty()) {\n                return BigInteger.ZERO;\n            }\n            try {\n                if (trimmed.length() > 2 && trimmed.charAt(0) == '0') {\n                    char c = trimmed.charAt(1);\n                    if (c == 'x' || c == 'X') return new BigInteger(trimmed.substring(2), 16);\n                    if (c == 'o' || c == 'O') return new BigInteger(trimmed.substring(2), 8);\n                    if (c == 'b' || c == 'B') return new BigInteger(trimmed.substring(2), 2);\n                }\n                return new BigInteger(trimmed);\n            } catch (NumberFormatException e) {\n                throw JsErrorException.syntaxError(\"Cannot convert \" + s + \" to a BigInt\");\n            }\n        }\n        if (value == null || value == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"Cannot convert \" + (value == null ? \"null\" : \"undefined\") + \" to a BigInt\");\n        }\n        throw JsErrorException.typeError(\"Cannot convert object to a BigInt\");\n    }\n\n    private Object asIntN(Context context, Object[] args) {\n        int bits = toIndex(args.length > 0 ? args[0] : Terms.UNDEFINED, (CoreContext) context);\n        BigInteger bi = toBigInt(args.length > 1 ? args[1] : Terms.UNDEFINED, (CoreContext) context);\n        if (bits == 0) return BigInteger.ZERO;\n        // mod 2^bits, then signed reinterpret\n        BigInteger mod = BigInteger.ONE.shiftLeft(bits);\n        BigInteger r = bi.mod(mod);\n        BigInteger half = BigInteger.ONE.shiftLeft(bits - 1);\n        if (r.compareTo(half) >= 0) {\n            r = r.subtract(mod);\n        }\n        return r;\n    }\n\n    private Object asUintN(Context context, Object[] args) {\n        int bits = toIndex(args.length > 0 ? args[0] : Terms.UNDEFINED, (CoreContext) context);","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java#L103-L139","documentation":"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.","triggerScenarios":"BigInt({}), BigInt([5]), BigInt(new Date()), BigInt(Symbol('x')), BigInt(someProxyOrClassInstance) — any value failing the Number/String/null/undefined checks in primitiveToBigInt.","commonSituations":"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.","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')"],"exampleFix":"// before\nlet bi = BigInt(payload.ids); // array -> TypeError\n// after\nlet bi = BigInt(payload.ids[0]);","handlingStrategy":"type-guard","validationCode":"function toBigIntSafe(x) {\n  if (x == null) return null;\n  if (typeof x === 'bigint') return x;\n  if (typeof x === 'number' && Number.isInteger(x)) return BigInt(x);\n  if (typeof x === 'string' && /^-?\\d+$/.test(x.trim())) return BigInt(x.trim());\n  return null; // objects, arrays, symbols\n}","typeGuard":"const isScalar = (x) => ['bigint','number','string'].includes(typeof x);","tryCatchPattern":"let bi;\ntry { bi = BigInt(x); } catch (e) {\n  if (e instanceof TypeError && /object/.test(e.message)) bi = BigInt(x.value ?? 0);\n  else throw e;\n}","preventionTips":["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"],"tags":["javascript","bigint","type-error","object-conversion"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}