{"record":{"id":"27a33aeb4614cea3","repo":"jackwener/OpenCLI","slug":"zhihu-search-limit-must-be-a-positive-integer-no","errorCode":null,"errorMessage":"zhihu search --limit must be a positive integer no greater than ${MAX_LIMIT}","messagePattern":"zhihu search --limit must be a positive integer no greater than (.+?)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/search.js","lineNumber":49,"sourceCode":"            return `https://www.zhihu.com/api/v4/search_v3${parsed.search}`;\n        }\n        if (parsed.hostname === 'www.zhihu.com' && parsed.pathname === '/api/v4/search_v3') {\n            return parsed.toString();\n        }\n    } catch {\n        return '';\n    }\n    return '';\n}\n\nconst MAX_LIMIT = 1000;\nconst PAGE_SIZE = 20;\nconst TYPES = ['all', 'answer', 'article', 'question'];\n\nfunction parseLimit(value) {\n    const limit = Number(value ?? 10);\n    if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_LIMIT) {\n        throw new ArgumentError(`zhihu search --limit must be a positive integer no greater than ${MAX_LIMIT}`, 'Use a normal-sized limit to avoid slow requests or Zhihu risk controls');\n    }\n    return limit;\n}\n\nfunction requireQuery(value) {\n    const query = String(value || '').trim();\n    if (!query) {\n        throw new ArgumentError('zhihu search query must not be empty', 'Example: opencli zhihu search codex');\n    }\n    return query;\n}\n\nfunction requireType(value) {\n    const type = String(value || 'all');\n    if (!TYPES.includes(type)) {\n        throw new ArgumentError(`zhihu search --type must be one of: ${TYPES.join(', ')}`, 'Example: opencli zhihu search codex --type answer');\n    }\n    return type;","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/search.js#L31-L67","documentation":"The --limit option passed to `zhihu search` failed parseLimit's validation: it must be a positive integer no greater than MAX_LIMIT. The CLI rejects non-integers, zero/negative values, and oversized limits to avoid slow requests and tripping Zhihu risk controls. This is thrown before any network request is made.","triggerScenarios":"Running `opencli zhihu search <query> --limit` with e.g. --limit 0, --limit -5, --limit abc, --limit 2.5, or any integer above MAX_LIMIT; also any non-numeric string (Number(value) yields NaN).","commonSituations":"Typo or stray characters in the flag value; copying a default like 100 from docs when MAX_LIMIT is smaller; shell interpolation producing an empty or malformed value; passing '10 ' with whitespace from a script.","solutions":["Pass a positive integer, e.g. --limit 20","Check the CLI's MAX_LIMIT constant in clis/zhihu/search.js and use a value at or below it","Quote or trim the value if it comes from a shell variable to avoid stray whitespace","Omit --limit entirely to use the default of 10"],"exampleFix":"// before\nopencli zhihu search codex --limit 500\n// after\nopencli zhihu search codex --limit 20","handlingStrategy":"validation","validationCode":"function safeLimit(v) {\n  const n = Number(v ?? 10);\n  if (!Number.isInteger(n) || n <= 0 || n > MAX_LIMIT) throw new Error(`--limit must be an integer 1..${MAX_LIMIT}`);\n  return n;\n}","typeGuard":"function isValidLimit(v) {\n  return Number.isInteger(v) && v > 0 && v <= MAX_LIMIT;\n}","tryCatchPattern":"try {\n  await runSearch({ limit: userLimit });\n} catch (err) {\n  if (err instanceof ArgumentError && err.message.includes('--limit')) {\n    console.error('Invalid --limit; using default 10.');\n    return runSearch({ limit: 10 });\n  }\n  throw err;\n}","preventionTips":["Always pass explicit small integer limits (10–20) when scripting","Clamp user-supplied limits with Math.min/Math.floor before invoking the CLI","Quote shell variables to prevent stray whitespace or empty values","Check MAX_LIMIT in clis/zhihu/search.js when increasing limits"],"tags":["cli","argument-validation","zhihu","input-error"],"backgroundTag":"invalid-cli-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}