{"record":{"id":"ef0730ce3320439d","repo":"sveltejs/kit","slug":"limit-must-be-a-non-negative-integer-or-infinity","errorCode":null,"errorMessage":"Limit must be a non-negative integer or Infinity","messagePattern":"Limit must be a non-negative integer or Infinity","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/app/server/remote/requested.js","lineNumber":245,"sourceCode":"\t\t\tfor await (const { ignore } of result) ignore();\n\t\t}\n\t};\n\n\treturn /** @type {RequestedResult<Validated, Output>} */ (/** @type {unknown} */ (result));\n}\n\n/**\n * @template T\n * @param {Array<T>} array\n * @param {number} limit\n * @returns {[Array<T>, Array<T>]}\n */\nfunction split_limit(array, limit) {\n\tif (limit === Infinity) {\n\t\treturn [array, []];\n\t}\n\tif (!Number.isInteger(limit) || limit < 0) {\n\t\tthrow new Error('Limit must be a non-negative integer or Infinity');\n\t}\n\treturn [array.slice(0, limit), array.slice(limit)];\n}\n\n/**\n * @param {any} value\n * @returns {value is PromiseLike<any>}\n */\nfunction is_thenable(value) {\n\treturn !!value && (typeof value === 'object' || typeof value === 'function') && 'then' in value;\n}\n\n/**\n * Runs all callbacks immediately and yields resolved values in completion order.\n * If the promise rejects, it is skipped.\n *\n * @template T\n * @template R","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/app/server/remote/requested.js#L227-L263","documentation":"`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.","triggerScenarios":"Calling `requested(query, limit)` with e.g. `-1`, `2.5`, `NaN`, `undefined`, a string like `'10'`, or `null` as the second argument.","commonSituations":"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.","solutions":["Coerce and validate the limit: `limit = Number.isFinite(limit) ? Math.max(0, Math.floor(limit)) : Infinity`.","Pass `Infinity` explicitly when no cap is wanted.","Default numeric config/env values, e.g. `Number(process.env.BATCH_LIMIT ?? 50)`."],"exampleFix":"// before\nconst limit = parseInt(url.searchParams.get('limit'));\nfor await (const r of requested(q, limit)) { ... }\n// after\nconst raw = parseInt(url.searchParams.get('limit') ?? '', 10);\nconst limit = Number.isFinite(raw) ? Math.max(0, raw) : Infinity;\nfor await (const r of requested(q, limit)) { ... }","handlingStrategy":"validation","validationCode":"function validLimit(n) {\n  return n === Infinity || (Number.isInteger(n) && n >= 0);\n}\nif (!validLimit(limit)) throw new RangeError('limit must be a non-negative integer or Infinity');","typeGuard":"function isLimit(v) { return v === Infinity || (typeof v === 'number' && Number.isInteger(v) && v >= 0); }","tryCatchPattern":"try {\n  for await (const item of requested(q, limit)) { /* ... */ }\n} catch (e) {\n  if (e.message.includes('Limit must be')) fallbackLimit();\n  else throw e;\n}","preventionTips":["Math.floor + clamp any user/config-derived limit.","Default missing numeric env/config values explicitly.","Use Number(...) not parseInt without radix when coercing."],"tags":["validation","arguments","batching","requested"],"backgroundTag":"invalid-argument-value","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}