{"record":{"id":"7d8ffd3d057cd3a7","repo":"karatelabs/karate","slug":"tostring-radix-must-be-between-2-and-36","errorCode":null,"errorMessage":"toString() radix must be between 2 and 36","messagePattern":"toString\\(\\) radix must be between 2 and 36","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntPrototype.java","lineNumber":63,"sourceCode":"        BigInteger n = asBigInt(context);\n        // Spec: only `undefined` (or absent) defaults to radix 10; `null` runs through\n        // ToInteger → 0 → RangeError. Same shape as Number.prototype.toString.\n        if (args.length > 0 && args[0] != Terms.UNDEFINED) {\n            Object radixArg = args[0];\n            // Object → ToPrimitive (hint \"number\"). If valueOf/toString are non-callable\n            // ToPrimitive throws TypeError (per spec); error flows through context for\n            // user-thrown valueOf bodies.\n            if (radixArg instanceof ObjectLike && context instanceof CoreContext cc) {\n                radixArg = Terms.toPrimitive(radixArg, \"number\", cc);\n                if (cc.isError()) return Terms.UNDEFINED;\n            }\n            // ToIntegerOrInfinity rejects BigInt — spec mandates TypeError before RangeError.\n            if (radixArg instanceof BigInteger) {\n                throw JsErrorException.typeError(\"Cannot convert a BigInt to a number\");\n            }\n            int radix = Terms.objectToNumber(radixArg).intValue();\n            if (radix < 2 || radix > 36) {\n                throw JsErrorException.rangeError(\"toString() radix must be between 2 and 36\");\n            }\n            return n.toString(radix);\n        }\n        return n.toString();\n    }\n\n    private Object valueOf(Context context, Object[] args) {\n        return asBigInt(context);\n    }\n\n    private static BigInteger asBigInt(Context context) {\n        Object thisObj = context.getThisObject();\n        if (thisObj instanceof JsBigInt jb) {\n            return jb.value;\n        }\n        if (thisObj instanceof BigInteger bi) {\n            return bi;\n        }","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntPrototype.java#L45-L81","documentation":"BigInt.prototype.toString(radix) requires the radix to be an integer between 2 and 36 inclusive; anything else throws this RangeError, mirroring Number.prototype.toString's restriction. Karate's JS engine applies the same bound after the BigInt-radix TypeError check.","triggerScenarios":"x.toString(1), x.toString(0), x.toString(37), x.toString(64), or x.toString(2.5); x.toString(radixFromConfig) with an unsupported base.","commonSituations":"Attempting base64/base58 encodings via toString; radix read from configuration as 0 or 1; typos like toString(16.) or negative radices; custom numeric encodings assuming unbounded bases.","solutions":["Clamp/validate: if (!Number.isInteger(r) || r < 2 || r > 36) r = 10","Use base36 (the maximum) plus a custom table for encodings beyond 36","For base64/base58 use a real encoder, not toString(radix)","Default the radix when undefined/invalid rather than passing it through"],"exampleFix":"// before\nlet s = id.toString(58); // RangeError\n// after\nlet s = !Number.isInteger(r) || r < 2 || r > 36 ? id.toString() : id.toString(r);","handlingStrategy":"validation","validationCode":"function bigIntToString(n, radix) {\n  if (radix === undefined) return n.toString();\n  if (!Number.isInteger(radix) || radix < 2 || radix > 36) throw new RangeError('radix must be 2-36');\n  return n.toString(radix);\n}","typeGuard":"const isValidRadix = (r) => Number.isInteger(r) && r >= 2 && r <= 36;","tryCatchPattern":"let s;\ntry { s = n.toString(radix); } catch (e) {\n  if (e instanceof RangeError && /2 and 36/.test(e.message)) s = n.toString();\n  else throw e;\n}","preventionTips":["Remember the spec caps radix at 36; use real encoders for base64/base58","Validate config-sourced radices against 2..36 at startup","Default to base 10 when radix is absent or invalid","Test toString(radix) calls with boundary values 1, 2, 36, 37"],"tags":["javascript","bigint","range-error","tostring-radix"],"backgroundTag":"value-out-of-range","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"}