karatelabs/karate · error · RangeError

Invalid code point

Error message

Invalid code point 

What it means

This RangeError is thrown by String.fromCodePoint when a numeric argument falls outside the valid Unicode code point range (0 to 0x10FFFF inclusive). Negative integers or values above the maximum code point cannot be encoded as a Unicode character, so the engine rejects them per spec (§22.1.2.2).

Solutions

  1. Validate code points before calling: if (n < 0 || n > 0x10FFFF) skip or clamp
  2. Mask sentinel values (e.g. stream read returning -1) to 0 or filter them out
  3. Use String.fromCodePoint(...codes.filter(isValidCodePoint)) to sanitize input arrays

Example fix

// before
const ch = String.fromCodePoint(-1); // RangeError
// after
const n = -1;
const ch = n >= 0 && n <= 0x10FFFF ? String.fromCodePoint(n) : '';
Defensive patterns

Strategy: validation

Validate before calling

const isValidCodePoint = (n) => Number.isInteger(n) && n >= 0 && n <= 0x10FFFF;

Try / catch

try { return String.fromCodePoint(...codes); } catch (e) { if (e.name === 'RangeError') return String.fromCodePoint(...codes.filter(isValidCodePoint)); throw e; }

Prevention

When it happens

Trigger: Calling String.fromCodePoint(-1), String.fromCodePoint(0x110000), or any argument that coerces to a number outside 0..1114111. Note the source only appends arguments that are instanceof Number after the check — non-number args are silently skipped, so the error fires specifically for out-of-range numbers.

Common situations: Computing code points from data (e.g. parsing hex from user input) that was not range-checked; off-by-one when iterating Unicode ranges; decoding binary data where sentinel values like -1 appear; using values above 0x10FFFF for astral-plane math.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/2bd715d92396bd8e. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsStringConstructor.java:74

    // Static methods

    private Object fromCharCode(Object[] args) {
        StringBuilder sb = new StringBuilder();
        for (Object arg : args) {
            if (arg instanceof Number num) {
                sb.append((char) num.intValue());
            }
        }
        return sb.toString();
    }

    private Object fromCodePoint(Object[] args) {
        StringBuilder sb = new StringBuilder();
        for (Object arg : args) {
            if (arg instanceof Number num) {
                int n = num.intValue();
                if (n < 0 || n > 0x10FFFF) {
                    throw JsErrorException.rangeError("Invalid code point " + num);
                }
                sb.appendCodePoint(n);
            }
        }
        return sb.toString();
    }

    // Spec §22.1.2.4 — String.raw(template, ...substitutions). Walks
    // template.raw[k] for k in [0, length), interleaving substitutions[k] from
    // the second arg onward. Coercion goes through the spec ToString helper so
    // host objects with a JS toString return user-visible strings, and
    // non-array-like raw values (length=NaN/0) fall through to the empty
    // string per §22.1.2.4 step 6 / 8.
    private Object raw(Context context, Object[] args) {
        Object template = args.length > 0 ? args[0] : Terms.UNDEFINED;
        Terms.requireObjectCoercible(template, "String.raw");
        if (!(template instanceof ObjectLike templateObj)) {
            throw JsErrorException.typeError("String.raw template must be an object");

View on GitHub (pinned to a22eb90246)