jackwener/OpenCLI · error · ArgumentError

zhihu collections --${name} must be a positive integer

Error message

zhihu collections --${name} must be a positive integer

What it means

validatePositiveInt in collections.js throws ArgumentError when a numeric option (limit/offset, referenced by name) is not an integer greater than 0. It is used to validate pagination arguments before requests are made, e.g. requestedLimit for the collections listing.

Source

Thrown at clis/zhihu/collections.js:8

import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { log } from '@jackwener/opencli/logger';

function validatePositiveInt(value, name) {
  const n = Number(value);
  if (!Number.isInteger(n) || n <= 0) {
    throw new ArgumentError(`zhihu collections --${name} must be a positive integer`, 'Example: opencli zhihu collections --limit 20');
  }
  return n;
}

async function fetchJson(page, url, errorLabel) {
  const data = await page.evaluate(`
    (async () => {
      const r = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
      if (!r.ok) return { __httpError: r.status };
      return await r.json();
    })()
  `);

  if (!data || data.__httpError) {
    const status = data?.__httpError;
    if (status === 401 || status === 403) {
      throw new AuthRequiredError('www.zhihu.com', `${errorLabel} from Zhihu failed. Please ensure you are logged in.`);
    }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a positive integer, e.g. opencli zhihu collections --limit 20
  2. Check the shell actually forwarded the value (quote it if it contains special chars)
  3. Use whole numbers without units or decimals
  4. For offsets that may be zero, use the command that validates non-negative ints instead

Example fix

// before
opencli zhihu collections --limit 0
// after
opencli zhihu collections --limit 20
Defensive patterns

Strategy: validation

Validate before calling

function assertPositiveInt(value, name) {
  const n = Number(value);
  if (!Number.isInteger(n) || n <= 0) throw new Error(`--${name} must be a positive integer, got: ${JSON.stringify(value)}`);
  return n;
}
assertPositiveInt(process.argv.limit, 'limit');

Type guard

function isPositiveInt(v) { return Number.isInteger(v) && v > 0; }

Try / catch

try {
  return await zhihuCollections({ limit: rawLimit });
} catch (e) {
  if (e instanceof ArgumentError && e.message.includes('must be a positive integer')) {
    return zhihuCollections({ limit: 20 });
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing --limit 0, --limit -5, a non-integer like --limit 2.5, or a non-numeric value like --limit abc / empty string.

Common situations: Copy-pasting values with units ('20 items'); shell quoting dropping the value so Number(undefined) is NaN; confusing offset semantics and passing 0 where a positive value is required.

Related errors


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