{"record":{"id":"a8b25ffa81f66e81","repo":"jackwener/OpenCLI","slug":"failed-to-fetch-zhihu-answer-detail","errorCode":null,"errorMessage":"Failed to fetch Zhihu answer detail","messagePattern":"Failed to fetch Zhihu answer detail","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":401,"severity":"error","filePath":"clis/zhihu/answer-detail.js","lineNumber":94,"sourceCode":"      (async () => {\n        const r = await fetch(${JSON.stringify(apiUrl)}, { credentials: 'include' });\n        if (!r.ok) return { __httpError: r.status };\n        try {\n          return await r.json();\n        } catch (error) {\n          return { __malformedJson: error instanceof Error ? error.message : String(error) };\n        }\n      })()\n    `).catch((err) => {\n            throw new CommandExecutionError(\n                `Zhihu answer detail request failed: ${err instanceof Error ? err.message : String(err)}`,\n                'Try again later or rerun with -v for more detail.',\n            );\n        });\n        if (!data || data.__httpError) {\n            const status = data?.__httpError;\n            if (status === 401 || status === 403) {\n                throw new AuthRequiredError('www.zhihu.com', 'Failed to fetch Zhihu answer detail');\n            }\n            if (status === 404) {\n                throw new EmptyResultError('zhihu answer-detail', `No Zhihu answer was found for ${answerId}.`);\n            }\n            throw new CommandExecutionError(\n                status\n                    ? `Zhihu answer detail request failed (HTTP ${status})`\n                    : 'Zhihu answer detail request failed',\n                'Try again later or rerun with -v for more detail',\n            );\n        }\n        if (data.__malformedJson) {\n            throw new CommandExecutionError(\n                `Zhihu answer detail returned malformed JSON: ${data.__malformedJson}`,\n                'Try again later or rerun with -v for more detail',\n            );\n        }\n        if (typeof data !== 'object' || Array.isArray(data)) {","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/answer-detail.js#L76-L112","documentation":"This AuthRequiredError is thrown when the Zhihu answer detail request reports HTTP 401 or 403, meaning the request was rejected due to missing or insufficient authentication. The library surfaces this as an auth error for www.zhihu.com rather than a generic failure, because the API requires a valid logged-in session/cookies for this endpoint.","triggerScenarios":"The in-page fetch returns __httpError === 401 or 403 from the Zhihu answer detail API — the browser profile has no zhihu.com session, cookies expired, or Zhihu's anti-bot gate rejected the request.","commonSituations":"Using a fresh browser profile that never logged into Zhihu, session cookies expired after weeks, Zhihu invalidated the session server-side, or an aggressive anti-crawler challenge returned 403.","solutions":["Log into www.zhihu.com in the browser profile the CLI uses, then rerun the command.","Refresh/renew Zhihu session cookies if they expired.","If 403 persists, slow down request rate or solve the anti-bot challenge manually in the browser first."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure a session exists before running\nconst cookies = await page.cookies('https://www.zhihu.com');\nif (!cookies.some(c => c.name === 'z_c0')) throw new Error('Not logged into zhihu.com — auth required');","typeGuard":"function isAuthRequiredError(err) { return err instanceof AuthRequiredError || /AuthRequired/.test(err?.name || ''); }","tryCatchPattern":"try {\n  const detail = await answerDetail(id);\n} catch (err) {\n  if (err instanceof AuthRequiredError) {\n    console.error('Login to www.zhihu.com in the browser profile, then rerun.');\n    process.exitCode = 3;\n    return;\n  }\n  throw err;\n}","preventionTips":["Log into zhihu.com in the browser profile the CLI uses before batch runs.","Check for a valid session cookie (z_c0) before starting long jobs.","Handle AuthRequiredError distinctly from network errors in automation scripts.","Rate-limit requests to avoid triggering 403 anti-bot responses."],"tags":["auth","http-401","http-403","zhihu"],"backgroundTag":"auth-required","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}