{"record":{"id":"136b3c6d08b478da","repo":"jackwener/OpenCLI","slug":"zhihu-search-query-must-not-be-empty","errorCode":null,"errorMessage":"zhihu search query must not be empty","messagePattern":"zhihu search query must not be empty","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/search.js","lineNumber":57,"sourceCode":"    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;\n}\n\nfunction unwrapEvaluateResult(payload) {\n    if (payload && typeof payload === 'object' && 'data' in payload && 'session' in payload) return payload.data;\n    return payload;\n}\n\nfunction requireSearchPayload(data, url) {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/search.js#L39-L75","documentation":"requireQuery in clis/zhihu/search.js throws ArgumentError when the search query string is empty after trimming. The CLI refuses to issue a Zhihu search with a blank query, since that request is meaningless and would fail server-side. Thrown before any network call.","triggerScenarios":"Running `opencli zhihu search` with no query argument, or `--query \"\"` / `--query \"   \"` where the value trims to empty.","commonSituations":"Scripting the CLI with an unquoted shell variable that is unset/empty (e.g. $Q missing); passing a value containing only whitespace; forgetting the positional argument.","solutions":["Provide a non-empty search term: opencli zhihu search codex","Quote the query in scripts so empty variables do not vanish as arguments","Guard the variable in scripts: [ -n \"$Q\" ] || exit 1 before invoking the CLI"],"exampleFix":"// before\nopencli zhihu search $Q   # Q is unset -> empty query\n// after\n[ -n \"$Q\" ] && opencli zhihu search \"$Q\"","handlingStrategy":"validation","validationCode":"const q = String(rawQuery ?? '').trim();\nif (!q) throw new Error('Search query must be a non-empty string, e.g. \"codex\"');","typeGuard":"function hasQuery(v) {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  await runSearch({ query: q });\n} catch (err) {\n  if (err instanceof ArgumentError && err.message.includes('query must not be empty')) {\n    console.error('Usage: opencli zhihu search <query>');\n    process.exitCode = 2;\n    return;\n  }\n  throw err;\n}","preventionTips":["Quote shell variables: opencli zhihu search \"$Q\"","Check for unset variables with : \"${Q:?Q must be set}\" in scripts","Trim and validate user input before passing it to the CLI"],"tags":["cli","argument-validation","zhihu","input-error"],"backgroundTag":"missing-required-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}