{"record":{"id":"087866a4a3568523","repo":"karatelabs/karate","slug":"cannot-convert-non-finite-number-to-bigint","errorCode":null,"errorMessage":"Cannot convert non-finite number to BigInt","messagePattern":"Cannot convert non-finite number to BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java","lineNumber":94,"sourceCode":"\n    // No-context overload for code paths that have only a primitive in hand\n    // (e.g. asIntN/asUintN second arg already coerced). Matches spec when the\n    // input is guaranteed primitive.\n    static BigInteger toBigInt(Object value) {\n        return toBigInt(value, null);\n    }\n\n    private static BigInteger primitiveToBigInt(Object value) {\n        if (value instanceof BigInteger bi) {\n            return bi;\n        }\n        if (value instanceof Boolean b) {\n            return b ? BigInteger.ONE : BigInteger.ZERO;\n        }\n        if (value instanceof Number n) {\n            double d = n.doubleValue();\n            if (!Double.isFinite(d)) {\n                throw JsErrorException.rangeError(\"Cannot convert non-finite number to BigInt\");\n            }\n            if (d != Math.floor(d)) {\n                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                }","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java#L76-L112","documentation":"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.","triggerScenarios":"BigInt(Infinity); BigInt(-Infinity); BigInt(NaN); BigInt(1.5) (non-integer variant); dividing by zero or parsing failing before a BigInt conversion.","commonSituations":"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.","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"],"exampleFix":"// before\nconst n = BigInt(total / count); // Infinity when count === 0\n// after\nif (!Number.isFinite(total / count) || !Number.isInteger(total / count)) throw new Error('bad input');\nconst n = BigInt(Math.trunc(total / count));","handlingStrategy":"validation","validationCode":"if (typeof v === 'number' && (!Number.isFinite(v) || !Number.isInteger(v))) throw new Error('BigInt needs a finite integer, got ' + v);","typeGuard":"function isSafeBigIntSource(x) { return typeof x === 'bigint' || (typeof x === 'number' && Number.isFinite(x) && Number.isInteger(x)) || (typeof x === 'string' && /^-?\\d+$/.test(x)); }","tryCatchPattern":"try { return BigInt(v); } catch (e) { if (String(e.message).includes('non-finite') || String(e.message).includes('non-integer')) return 0n; throw e; }","preventionTips":["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"],"tags":["javascript","bigint","range-error"],"backgroundTag":"invalid-argument-value","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"}