jackwener/OpenCLI · error · ArgumentError

Collection ID must be numeric

Error message

Collection ID must be numeric

What it means

The command's data handler throws ArgumentError when the id kwarg is not a purely numeric string, checked with /^\d+$/. Zhihu collection IDs are numeric, so any non-numeric input is rejected before any request is made.

Source

Thrown at clis/zhihu/collection.js:126

    access: 'read',
  description: '知乎收藏夹内容列表(需要登录)',
  domain: 'www.zhihu.com',
  strategy: Strategy.COOKIE,
  browser: true,
  args: [
    { name: 'id', positional: true, required: true, help: '收藏夹 ID (数字,可从收藏夹 URL 中获取)' },
    { name: 'offset', type: 'int', default: 0, help: '起始偏移量(用于分页)' },
    { name: 'limit', type: 'int', default: 20, help: '每页数量(最大 20)' },
  ],
  columns: ['rank', 'type', 'title', 'author', 'votes', 'excerpt', 'url'],
  func: async (page, kwargs) => {
    const { id, offset = 0, limit = 20 } = kwargs;

    const collectionId = String(id);

    // 验证收藏夹 ID 为数字
    if (!/^\d+$/.test(collectionId)) {
      throw new ArgumentError('Collection ID must be numeric', 'Example: opencli zhihu collection 83283292');
    }

    const pageOffset = validateNonNegativeInt(offset, 'offset');
    const requestedLimit = validatePositiveInt(limit, 'limit');
    const pageLimit = Math.min(requestedLimit, 20); // 知乎 API 限制每页最大 20

    // 先访问知乎主页建立 session
    await page.goto('https://www.zhihu.com');

    const collected = [];
    const seen = new Set();
    let totals = 0;
    let nextOffset = pageOffset;
    const maxPages = Math.ceil(requestedLimit / pageLimit) + 2;
    for (let pageIndex = 0; pageIndex < maxPages && collected.length < requestedLimit; pageIndex += 1) {
      const currentFetchLimit = Math.min(pageLimit, requestedLimit - collected.length);
      const data = await fetchCollectionPage(page, collectionId, nextOffset, currentFetchLimit);
      const items = Array.isArray(data.data) ? data.data : [];

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass only the numeric collection ID, e.g. opencli zhihu collection 83283292
  2. If you have a URL, extract the digits after /collection/ before invoking
  3. Trim the input to remove stray whitespace
  4. Check you are not using a different entity's ID (question/article)

Example fix

// before
opencli zhihu collection "https://www.zhihu.com/collection/83283292"
// after
opencli zhihu collection 83283292
Defensive patterns

Strategy: validation

Validate before calling

function assertNumericCollectionId(id) {
  const s = String(id ?? '').trim();
  if (!/^\d+$/.test(s)) throw new Error(`collection id must be numeric, got: ${s}`);
  return s;
}

Type guard

function isNumericId(v) { return typeof v === 'string' || typeof v === 'number' ? /^\d+$/.test(String(v)) : false; }

Try / catch

try {
  return await zhihuCollection(rawId);
} catch (e) {
  if (e instanceof ArgumentError) {
    const m = /collection\/(\d+)/.exec(String(rawId));
    if (m) return zhihuCollection(m[1]);
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a collection URL instead of its ID (e.g. 'https://www.zhihu.com/collection/83283292'), a value with whitespace, letters, or an empty string as id.

Common situations: Pasting the whole collection URL from the browser address bar; copying an ID with trailing whitespace or invisible characters; mixing up question IDs and collection IDs.

Related errors


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