jackwener/OpenCLI · error · ArgumentError

limit must be a positive integer (1-${max})

Error message

limit must be a positive integer (1-${max})

What it means

normalizeLimit() requires the limit to be a positive integer (>= 1) when provided. Non-integers, zero, negatives, and non-numeric strings throw this ArgumentError with the allowed range 1..max.

Source

Thrown at clis/12306/utils.js:108

}

export function validateDate(value) {
    if (!DATE_RE.test(String(value ?? ''))) {
        throw new ArgumentError(`date must be YYYY-MM-DD, got "${value}"`);
    }
    const [y, m, d] = value.split('-').map(Number);
    const date = new Date(Date.UTC(y, m - 1, d));
    if (date.getUTCFullYear() !== y || date.getUTCMonth() !== m - 1 || date.getUTCDate() !== d) {
        throw new ArgumentError(`date "${value}" is not a real calendar date`);
    }
    return value;
}

export function normalizeLimit(value, defaultValue, max) {
    if (value === undefined || value === null || value === '') return defaultValue;
    const n = Number(value);
    if (!Number.isInteger(n) || n < 1) {
        throw new ArgumentError(`limit must be a positive integer (1-${max})`);
    }
    if (n > max) {
        throw new ArgumentError(`limit must be <= ${max}`);
    }
    return n;
}

/** Extract Set-Cookie header values into a single `Cookie:` header string. */
export function buildCookieHeader(setCookieHeaders) {
    if (!Array.isArray(setCookieHeaders) || setCookieHeaders.length === 0) return '';
    return setCookieHeaders
        .map((line) => line.split(';')[0])
        .filter(Boolean)
        .join('; ');
}

export async function fetchStationBundle(fetchImpl = fetch) {
    const resp = await fetchImpl(STATION_BUNDLE_URL, {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a positive integer between 1 and max, or omit the option entirely to get the default
  2. Round/ceiling computed values with Math.floor/Math.ceil before passing
  3. Treat 'unlimited' as omitting limit rather than 0

Example fix

// before
await query({ limit: 0 });
// after
await query({}); // default limit
// or
await query({ limit: Math.floor(pageSize) });
Defensive patterns

Strategy: validation

Validate before calling

function isValidLimit(v, max) {
  if (v === undefined || v === null || v === '') return true; // default applies
  const n = Number(v);
  return Number.isInteger(n) && n >= 1 && n <= max;
}
if (!isValidLimit(opts.limit, 100)) throw new Error('limit must be a positive integer');

Try / catch

try {
  await query({ limit });
} catch (e) {
  if (e instanceof ArgumentError && e.message.startsWith('limit must be a positive integer')) {
    console.error('Supply an integer limit >= 1, or omit it for the default.');
  } else throw e;
}

Prevention

When it happens

Trigger: Passing limit=0, limit=-5, limit=2.5, limit='ten', or limit='3.0' (string that Number() parses to a non-integer-safe value is fine as 3; '2.5' fails) to any command exposing a limit option.

Common situations: Config files with limit: 0 meaning 'unlimited' (wrong assumption here), page sizes computed by division yielding floats, or users entering 0 intending 'no limit'.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/d1804b5aab8b8ec5. Report an issue: GitHub.