{"record":{"id":"1114b121150219be","repo":"jackwener/OpenCLI","slug":"zhihu-whoami-failed-data-exception","errorCode":null,"errorMessage":"Zhihu whoami failed: ${data.__exception}","messagePattern":"Zhihu whoami failed: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/auth.js","lineNumber":27,"sourceCode":"async function verifyZhihuIdentity(page) {\n  if (!await hasZhihuAuthCookie(page)) {\n    throw new AuthRequiredError('www.zhihu.com', 'Zhihu z_c0 cookie missing — anonymous');\n  }\n  await page.goto('https://www.zhihu.com/');\n  await page.wait(2);\n  const data = await page.evaluate(`\n    (async () => {\n      try {\n        const r = await fetch('https://www.zhihu.com/api/v4/me?include=url_token', { credentials: 'include' });\n        if (!r.ok) return { __httpError: r.status };\n        return await r.json();\n      } catch (e) {\n        return { __exception: String(e && e.message || e) };\n      }\n    })()\n  `);\n  if (data?.__exception) {\n    throw new CommandExecutionError(`Zhihu whoami failed: ${data.__exception}`);\n  }\n  if (!data || data.__httpError) {\n    const status = data?.__httpError;\n    if (status === 401 || status === 403) {\n      throw new AuthRequiredError('www.zhihu.com', `Zhihu /api/v4/me returned HTTP ${status} — anonymous`);\n    }\n    throw new CommandExecutionError(`Zhihu identity probe failed (HTTP ${status ?? 'unknown'})`);\n  }\n  if (!data.url_token) {\n    throw new AuthRequiredError('www.zhihu.com', 'Zhihu /api/v4/me returned no url_token — anonymous session');\n  }\n  return {\n    url_token: String(data.url_token),\n    name: String(data.name ?? ''),\n    uid: String(data.uid ?? data.id ?? ''),\n  };\n}\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/auth.js#L9-L45","documentation":"During identity verification the CLI evaluates a 'whoami' script inside the Zhihu page that calls /api/v4/me inside a try/catch returning { __exception } on any in-page failure. If __exception is present, the script itself blew up (not just an HTTP error), and the library rethrows it as CommandExecutionError with the original message.","triggerScenarios":"The injected page script throws before producing a normal result — e.g. fetch itself rejects (network/DNS/redirect to login page altering globals), JSON parsing of an unexpected response, or page environment lacking expected APIs (script blocked by extension/CSP).","commonSituations":"Corporate proxy or captive portal intercepting the request inside the page; a browser extension or CSP blocking fetch to /api/v4/me; the page navigated away mid-evaluate; Zhihu serving an anti-bot interstitial that breaks the script.","solutions":["Open the automated browser and check https://www.zhihu.com loads normally and /api/v4/me is reachable (no proxy/extension interference).","Re-run the command after ensuring the page finished loading; transient navigation during evaluate causes in-page exceptions.","Disable interfering browser extensions or CSP blocks in the automation profile.","If the exception indicates network failure, fix connectivity/proxy settings and retry."],"exampleFix":"// before\nconst data = await verifyZhihuIdentity(page); // CommandExecutionError: Zhihu whoami failed: TypeError: fetch failed\n// after\ntry {\n  const data = await verifyZhihuIdentity(page);\n} catch (e) {\n  if (String(e.message).includes('whoami failed')) {\n    await page.goto('https://www.zhihu.com/'); // ensure page is loaded and healthy\n    await sleep(2000);\n    return verifyZhihuIdentity(page); // one retry\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"await page.goto('https://www.zhihu.com/', { waitUntil: 'load' }); // ensure healthy page before in-page whoami probe","typeGuard":"function isInPageResult(d) { return d !== null && typeof d === 'object' && !('__exception' in d); }","tryCatchPattern":"try { const me = await verifyZhihuIdentity(page); } catch (e) { if (String(e.message).startsWith('Zhihu whoami failed:')) { console.error('In-page script failed:', e.message); await sleep(2000); return retryOnce(); } throw e; }","preventionTips":["Disable extensions/CSP interference in the automation profile","Ensure network/proxy allows www.zhihu.com and /api/v4/me","Wait for page load before evaluate calls","Retry once on transient in-page failures"],"tags":["zhihu","in-page-script","network","command-execution"],"backgroundTag":"in-page-script-exception","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}