karatelabs/karate · error · RangeError

Invalid count value

Error message

Invalid count value

What it means

This RangeError is thrown by String.prototype.repeat when the count argument is negative or +Infinity. A NaN count yields the empty string per spec, but negative and infinite counts have no valid result, so the engine raises RangeError before clamping to an integer.

Solutions

  1. Clamp the count before calling: count = Math.max(0, Math.min(count, maxLen/str.length))
  2. Check Math.isFinite(count) && count >= 0 before repeating
  3. Cap counts derived from division to avoid Infinity (guard the divisor)

Example fix

// before
const pad = ' '.repeat(width - s.length); // RangeError if width < s.length
// after
const pad = ' '.repeat(Math.max(0, width - s.length));
Defensive patterns

Strategy: validation

Validate before calling

const safeRepeat = (s, n) => (Number.isFinite(n) && n >= 0 ? s.repeat(Math.floor(n)) : '');

Type guard

const isValidCount = (n) => Number.isFinite(n) && n >= 0;

Try / catch

try { return s.repeat(count); } catch (e) { if (e.name === 'RangeError') return ''; throw e; }

Prevention

When it happens

Trigger: Calling 'x'.repeat(-1) or 'x'.repeat(Infinity); passing a computed count from division (e.g. total/size that is negative or infinite); counts derived from NaN-free but unbounded math.

Common situations: Padding logic where the count calculation divides by zero (→ Infinity); user input used directly as repeat count; integer underflow producing negative values; porting code where counts were previously truncated.

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/4bdf76104ef96603. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsStringPrototype.java:357

        int padLength = targetLength - s.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < padLength; i++) {
            sb.append(padString.charAt(i % padString.length()));
        }
        sb.append(s);
        return sb.toString();
    }

    private Object repeat(Context context, Object[] args) {
        String s = thisString(context, "repeat");
        // Spec: ToIntegerOrInfinity, then RangeError if < 0 or +Infinity.
        // We only carry double precision through argInt; check the original
        // numeric for the -0 / +Infinity edge before clamping.
        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) return "";
        double d = Terms.objectToNumber(args[0]).doubleValue();
        if (Double.isNaN(d)) return "";
        if (d < 0 || Double.isInfinite(d)) {
            throw JsErrorException.rangeError("Invalid count value");
        }
        int count = (int) d;
        return s.repeat(count);
    }

    private Object slice(Context context, Object[] args) {
        String s = thisString(context, "slice");
        int beginIndex = argInt(args, 0, 0);
        int endIndex = (args.length > 1 && args[1] != Terms.UNDEFINED) ? argInt(args, 1, s.length()) : s.length();
        // handle negative indices
        if (beginIndex < 0) beginIndex = Math.max(s.length() + beginIndex, 0);
        if (endIndex < 0) endIndex = Math.max(s.length() + endIndex, 0);
        // ensure proper range
        beginIndex = Math.min(beginIndex, s.length());
        endIndex = Math.min(endIndex, s.length());
        if (beginIndex >= endIndex) return "";
        return s.substring(beginIndex, endIndex);
    }

View on GitHub (pinned to a22eb90246)