{"record":{"id":"d81ea3e56a3c0878","repo":"jackwener/OpenCLI","slug":"zhihu-search-returned-malformed-data-list","errorCode":null,"errorMessage":"Zhihu search returned malformed data list","messagePattern":"Zhihu search returned malformed data list","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/search.js","lineNumber":91,"sourceCode":"}\n\nfunction requireSearchPayload(data, url) {\n    const payload = unwrapEvaluateResult(data);\n    if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {\n        throw new CommandExecutionError('Zhihu search returned malformed payload');\n    }\n    if (payload.__httpError) {\n        const status = payload.__httpError;\n        if (status === 401 || status === 403) {\n            throw new AuthRequiredError('www.zhihu.com', 'Failed to fetch search results from Zhihu');\n        }\n        throw new CommandExecutionError(`Zhihu search request failed${status ? ` (HTTP ${status})` : ''}`, 'Try again later or rerun with -v for more detail');\n    }\n    if (payload.__fetchError) {\n        throw new CommandExecutionError('Zhihu search request failed', String(payload.__fetchError));\n    }\n    if (!Array.isArray(payload.data)) {\n        throw new CommandExecutionError('Zhihu search returned malformed data list', `URL: ${url}`);\n    }\n    if (!payload.paging || typeof payload.paging !== 'object') {\n        throw new CommandExecutionError('Zhihu search returned malformed paging data', `URL: ${url}`);\n    }\n    return payload;\n}\n\nfunction normalizeResultItem(item) {\n    if (!item || typeof item !== 'object' || item.type !== 'search_result' || !item.object || typeof item.object !== 'object') {\n        return null;\n    }\n    const obj = item.object;\n    if (obj.type !== 'answer' && obj.type !== 'article' && obj.type !== 'question') return null;\n    const key = itemKey(item);\n    const url = itemUrl(obj);\n    const question = obj.question || {};\n    const title = stripHtml(obj.title || question.name || question.title || '');\n    if (!key || !url || !title) {","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/search.js#L73-L109","documentation":"requireSearchPayload validated that the response's payload.data field is an array and it was not (missing, null, or another type). The CLI throws CommandExecutionError because it cannot iterate results without a list, even though the overall payload shape was otherwise acceptable.","triggerScenarios":"Zhihu's search endpoint returned 200/valid JSON but without the expected data array — e.g. an empty object, a result wrapper renamed upstream, or the in-page script built the payload incorrectly when zero results matched an unusual query.","commonSituations":"Zhihu silently changing the search API response schema; queries returning no hits but also omitting the data key; API responses intercepted by an extension modifying the JSON.","solutions":["Retry with a different, more common query to rule out a zero-result edge case","Rerun with -v and inspect the raw payload to see what shape actually arrived","Update the CLI if Zhihu changed the response schema (check for a newer version)","Disable browser extensions that rewrite page/network responses and retry"],"exampleFix":"// before\nconst items = results.data.map(normalizeResultItem);\n// after\nif (!Array.isArray(results?.data)) throw new Error('Unexpected Zhihu search shape');\nconst items = results.data.map(normalizeResultItem);","handlingStrategy":"type-guard","validationCode":"const payload = unwrapEvaluateResult(rawResponse);\nif (!payload || typeof payload !== 'object' || !Array.isArray(payload.data)) {\n  throw new Error('Zhihu response lacks a data array; aborting result processing');\n}","typeGuard":"function hasDataArray(payload) {\n  return !!payload && typeof payload === 'object' && !Array.isArray(payload) && Array.isArray(payload.data);\n}","tryCatchPattern":"try {\n  const results = await searchZhihu(q);\n} catch (err) {\n  if (err instanceof CommandExecutionError && err.message === 'Zhihu search returned malformed data list') {\n    console.error('Zhihu response schema may have changed; update the CLI or retry with a different query.');\n    return [];\n  }\n  throw err;\n}","preventionTips":["Pin and update the CLI so response-schema changes upstream are picked up","Treat payload.data defensively in downstream code even when the CLI returns successfully","Test searches with both common and obscure queries to catch zero-result edge cases","Disable extensions that mutate JSON responses during development"],"tags":["zhihu","schema-mismatch","malformed-payload","parsing"],"backgroundTag":"schema-validation-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}