santifer/career-ops · error · RangeError

ratePerMin must be a finite number > 0, got

Error message

ratePerMin must be a finite number > 0, got ${ratePerMin}

What it means

createTokenBucket in providers/_dns-cache.mjs validates its ratePerMin parameter with a RangeError when it is not a finite number greater than zero. A zero or negative rate would make the refill interval Infinity and hang every queued DNS lookup, so the bucket refuses to construct rather than deadlock later. The comment in source marks this as caller-supplied input that must be validated rather than asserted.

Solutions

  1. Pass a positive finite number for ratePerMin (e.g. 60 for 60 lookups/min).
  2. Coerce config values with Number(value) and check Number.isFinite before constructing the bucket.
  3. Fix the config source that produced 0/NaN — a missing or misnamed rate-limit key.
  4. Sanity-check the default: if the caller computes the rate, ensure its denominator is non-zero.

Example fix

// before
const bucket = createTokenBucket(config.dnsRate, capacity)
// after
const rate = Number(config.dnsRate)
if (!Number.isFinite(rate) || rate <= 0) throw new Error(`dnsRate must be > 0, got ${config.dnsRate}`)
const bucket = createTokenBucket(rate, capacity)
Defensive patterns

Strategy: validation

Validate before calling

const rate = Number(rawRate);
if (!Number.isFinite(rate) || rate <= 0) {
  throw new RangeError(`ratePerMin must be a finite number > 0, got ${rawRate}`);
}

Type guard

function isPositiveFinite(n) {
  return typeof n === 'number' && Number.isFinite(n) && n > 0;
}

Try / catch

let bucket;
try {
  bucket = createTokenBucket(rate, capacity);
} catch (e) {
  if (e instanceof RangeError) {
    console.error(`Bad rate config: ${e.message}; falling back to default 60/min`);
    bucket = createTokenBucket(60, capacity);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling createTokenBucket(ratePerMin, ...) with NaN, Infinity, 0, a negative number, or a non-numeric value (e.g. a string from config parsed without Number()).

Common situations: A rate limit read from config/env as a string ('60' vs 60); a config field left as 0 or null; division upstream producing NaN; YAML parsing a value as a string.

Related errors


AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16). Data as JSON: /api/errors/f7c0b2a8c5289a66. Report an issue: GitHub.

Appendix: source

Thrown at providers/_dns-cache.mjs:136

 * Clock and timer are injectable so the tests are deterministic and offline.
 *
 * @param {object} [options] - Bucket tuning.
 * @param {number} [options.ratePerMin] - Sustained calls per minute. Must be > 0.
 * @param {number} [options.capacity] - Burst size, in tokens.
 * @param {() => number} [options.now] - Clock source, injectable for tests.
 * @param {(fn: Function, ms: number) => void} [options.setTimer] - Timer, injectable for tests.
 * @returns {{ take: (fn: Function) => void, pending: number, stats: () => { delayed: number, waitedMs: number } }}
 */
export function createTokenBucket(options = {}) {
  const ratePerMin = options.ratePerMin ?? DEFAULT_LOOKUPS_PER_MIN;
  const capacity = options.capacity ?? DEFAULT_BURST;
  const now = options.now ?? Date.now;
  const setTimer = options.setTimer ?? setTimeout;

  // Caller-supplied, so validate rather than assert: a zero or negative rate
  // would make the refill interval Infinity and hang every queued lookup.
  if (!Number.isFinite(ratePerMin) || ratePerMin <= 0) {
    throw new RangeError(`ratePerMin must be a finite number > 0, got ${ratePerMin}`);
  }
  if (!Number.isFinite(capacity) || capacity < 1) {
    throw new RangeError(`capacity must be a finite number >= 1, got ${capacity}`);
  }

  const tokensPerMs = ratePerMin / 60_000;
  let tokens = capacity;
  let lastRefill = now();
  /** @type {{ fn: Function, queuedAt: number }[]} */
  const queue = [];
  let timerPending = false;
  let delayed = 0;
  let waitedMs = 0;

  function refill() {
    const t = now();
    tokens = Math.min(capacity, tokens + (t - lastRefill) * tokensPerMs);
    lastRefill = t;

View on GitHub (pinned to aac998c7ed)