sveltejs/kit · error

Limit must be a non-negative integer or Infinity

Error message

Limit must be a non-negative integer or Infinity

What it means

`requested(fn, limit)` splits its payload list via `split_limit`, which requires the limit to be a non-negative integer or `Infinity`. Any other value — fractional, negative, NaN, non-number — is rejected before iteration starts.

Source

Thrown at packages/kit/src/runtime/app/server/remote/requested.js:245

			for await (const { ignore } of result) ignore();
		}
	};

	return /** @type {RequestedResult<Validated, Output>} */ (/** @type {unknown} */ (result));
}

/**
 * @template T
 * @param {Array<T>} array
 * @param {number} limit
 * @returns {[Array<T>, Array<T>]}
 */
function split_limit(array, limit) {
	if (limit === Infinity) {
		return [array, []];
	}
	if (!Number.isInteger(limit) || limit < 0) {
		throw new Error('Limit must be a non-negative integer or Infinity');
	}
	return [array.slice(0, limit), array.slice(limit)];
}

/**
 * @param {any} value
 * @returns {value is PromiseLike<any>}
 */
function is_thenable(value) {
	return !!value && (typeof value === 'object' || typeof value === 'function') && 'then' in value;
}

/**
 * Runs all callbacks immediately and yields resolved values in completion order.
 * If the promise rejects, it is skipped.
 *
 * @template T
 * @template R

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Coerce and validate the limit: `limit = Number.isFinite(limit) ? Math.max(0, Math.floor(limit)) : Infinity`.
  2. Pass `Infinity` explicitly when no cap is wanted.
  3. Default numeric config/env values, e.g. `Number(process.env.BATCH_LIMIT ?? 50)`.

Example fix

// before
const limit = parseInt(url.searchParams.get('limit'));
for await (const r of requested(q, limit)) { ... }
// after
const raw = parseInt(url.searchParams.get('limit') ?? '', 10);
const limit = Number.isFinite(raw) ? Math.max(0, raw) : Infinity;
for await (const r of requested(q, limit)) { ... }
Defensive patterns

Strategy: validation

Validate before calling

function validLimit(n) {
  return n === Infinity || (Number.isInteger(n) && n >= 0);
}
if (!validLimit(limit)) throw new RangeError('limit must be a non-negative integer or Infinity');

Type guard

function isLimit(v) { return v === Infinity || (typeof v === 'number' && Number.isInteger(v) && v >= 0); }

Try / catch

try {
  for await (const item of requested(q, limit)) { /* ... */ }
} catch (e) {
  if (e.message.includes('Limit must be')) fallbackLimit();
  else throw e;
}

Prevention

When it happens

Trigger: Calling `requested(query, limit)` with e.g. `-1`, `2.5`, `NaN`, `undefined`, a string like `'10'`, or `null` as the second argument.

Common situations: Computing the limit from a user input or config value without coercion; a missing env var producing `undefined`/`NaN`; passing a page size from `Math.floor`-less division.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/ef0730ce3320439d. Report an issue: GitHub.