{"record":{"id":"0e877cd5a3936ec2","repo":"karatelabs/karate","slug":"cannot-convert-to-a-bigint","errorCode":null,"errorMessage":"Cannot convert  to a BigInt","messagePattern":"Cannot convert  to a BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java","lineNumber":115,"sourceCode":"                throw JsErrorException.rangeError(\"Cannot convert non-integer number to BigInt\");\n            }\n            return new BigDecimal(d).toBigInteger();\n        }\n        if (value instanceof String s) {\n            String trimmed = s.trim();\n            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);","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java#L97-L133","documentation":"BigInt() called with a string that is not a valid integer literal (empty, whitespace-only, decimal point, garbage characters) throws this SyntaxError per ECMAScript StringToBigInt. Karate's JS engine surfaces the original string in the message.","triggerScenarios":"BigInt('') or BigInt('   '), BigInt('3.14'), BigInt('abc'), BigInt('12px') — any string whose new BigInteger(trimmed) parse throws NumberFormatException; empty strings produce the empty-value message 'Cannot convert  to a BigInt'.","commonSituations":"Parsing numeric strings from APIs, CSV cells, or form inputs that are blank or contain units/decimals; JSON keys assumed numeric but holding '12.5' or empty values.","solutions":["Trim and test the string against /^-?\\d+$/ before calling BigInt(s)","Handle empty strings explicitly (default to 0n or throw your own clear error)","Strip non-numeric suffixes (units, currency symbols) first: BigInt(s.replace(/[^-\\d]/g, ''))","For decimal strings, decide on scaling: BigInt(Math.trunc(Number('3.14'))) or work in minor units BigInt('314')"],"exampleFix":"// before\nlet id = BigInt(row.id); // row.id = '' -> SyntaxError\n// after\nlet id = /^-?\\d+$/.test(row.id) ? BigInt(row.id) : 0n;","handlingStrategy":"validation","validationCode":"function stringToBigInt(s) {\n  if (typeof s !== 'string') return null;\n  const t = s.trim();\n  return /^-?\\d+$/.test(t) ? BigInt(t) : null;\n}","typeGuard":"const isBigIntString = (s) => typeof s === 'string' && /^-?\\d+$/.test(s.trim());","tryCatchPattern":"let bi;\ntry { bi = BigInt(s); } catch (e) {\n  if (e instanceof SyntaxError) bi = 0n; // or surface a domain error\n  else throw e;\n}","preventionTips":["Validate with /^-?\\d+$/ before parsing numeric strings","Trim and reject empty/whitespace strings explicitly","Strip units/currency symbols before conversion","Convert decimal strings via a documented scaling strategy, never directly"],"tags":["javascript","bigint","syntax-error","string-parsing"],"backgroundTag":"invalid-argument-format","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"}