{"record":{"id":"c47eb8a41490ecf4","repo":"karatelabs/karate","slug":"cannot-convert-undefined-to-a-bigint","errorCode":null,"errorMessage":"Cannot convert undefined to a BigInt","messagePattern":"Cannot convert undefined to a BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java","lineNumber":52,"sourceCode":"class JsBigIntConstructor extends JsFunction {\n    private static final byte METHOD_ATTRS = WRITABLE | CONFIGURABLE | PropertySlot.INTRINSIC;\n\n    JsBigIntConstructor() {\n        this.name = \"BigInt\";\n        this.length = 1;\n        installIntrinsics();\n    }\n\n    private void installIntrinsics() {\n        defineOwn(\"asIntN\", new JsBuiltinMethod(\"asIntN\", 2, this::asIntN), METHOD_ATTRS);\n        defineOwn(\"asUintN\", new JsBuiltinMethod(\"asUintN\", 2, this::asUintN), METHOD_ATTRS);\n        defineOwn(\"prototype\", JsBigIntPrototype.INSTANCE, PropertySlot.INTRINSIC);\n    }\n\n    @Override\n    public Object call(Context context, Object[] args) {\n        if (args.length == 0) {\n            throw JsErrorException.typeError(\"Cannot convert undefined to a BigInt\");\n        }\n        return toBigInt(args[0], (CoreContext) context);\n    }\n\n    /**\n     * Spec ToBigInt: ToPrimitive with hint \"number\", then convert. ToPrimitive\n     * fires only on the rare ObjectLike path — primitive inputs (Number, String,\n     * Boolean, BigInt) skip it entirely.\n     */\n    static BigInteger toBigInt(Object value, CoreContext context) {\n        // Rare path: object → call valueOf / toString to get a primitive\n        if (value instanceof ObjectLike) {\n            value = Terms.toPrimitive(value, \"number\", context);\n            if (context != null && context.isError()) {\n                // Caller will surface the propagated error; sentinel return is OK\n                // since the host will throw before reading it.\n                return BigInteger.ZERO;\n            }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java#L34-L70","documentation":"BigInt() called with no arguments has nothing to convert; per spec the conversion of undefined to BigInt is illegal, so Karate throws TypeError 'Cannot convert undefined to a BigInt' from the constructor's call path.","triggerScenarios":"BigInt() with an empty argument list; BigInt(someUndefinedVariable); calling a wrapper that forwards no arguments to BigInt; destructuring that omitted the value.","commonSituations":"Missing function parameters, JSON fields absent from a payload, refactored code where an argument was renamed, template-generated expressions that dropped the value.","solutions":["Pass a value: BigInt(123) or BigInt('123')","Guard the argument: if (v === undefined) return 0n;","Default the parameter: function toBig(v = 0) { return BigInt(v); }","Fix the caller that stopped supplying the argument"],"exampleFix":"// before\nconst n = BigInt();\n// after\nconst n = BigInt(0);","handlingStrategy":"validation","validationCode":"if (v === undefined || v === null) throw new Error('BigInt requires a value');","typeGuard":"function isBigIntConvertible(x) { return typeof x === 'number' ? Number.isFinite(x) && Number.isInteger(x) : typeof x === 'string' ? /^-?\\d+$/.test(x) : typeof x === 'bigint'; }","tryCatchPattern":"try { return BigInt(v); } catch (e) { if (String(e.message).includes('undefined')) return 0n; throw e; }","preventionTips":["Never call BigInt() with zero arguments","Default parameters that feed BigInt conversions","Validate payload fields exist before converting to BigInt"],"tags":["javascript","bigint","type-error"],"backgroundTag":"missing-required-argument","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"}