jackwener/OpenCLI · error · CommandExecutionError

Zhihu whoami failed

Error message

Zhihu whoami failed: ${data.__exception}

What it means

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.

Solutions

  1. Open the automated browser and check https://www.zhihu.com loads normally and /api/v4/me is reachable (no proxy/extension interference).
  2. Re-run the command after ensuring the page finished loading; transient navigation during evaluate causes in-page exceptions.
  3. Disable interfering browser extensions or CSP blocks in the automation profile.
  4. If the exception indicates network failure, fix connectivity/proxy settings and retry.

Example fix

// before
const data = await verifyZhihuIdentity(page); // CommandExecutionError: Zhihu whoami failed: TypeError: fetch failed
// after
try {
  const data = await verifyZhihuIdentity(page);
} catch (e) {
  if (String(e.message).includes('whoami failed')) {
    await page.goto('https://www.zhihu.com/'); // ensure page is loaded and healthy
    await sleep(2000);
    return verifyZhihuIdentity(page); // one retry
  }
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

await page.goto('https://www.zhihu.com/', { waitUntil: 'load' }); // ensure healthy page before in-page whoami probe

Type guard

function isInPageResult(d) { return d !== null && typeof d === 'object' && !('__exception' in d); }

Try / catch

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; }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/1114b121150219be. Report an issue: GitHub.

Appendix: source

Thrown at clis/zhihu/auth.js:27

async function verifyZhihuIdentity(page) {
  if (!await hasZhihuAuthCookie(page)) {
    throw new AuthRequiredError('www.zhihu.com', 'Zhihu z_c0 cookie missing — anonymous');
  }
  await page.goto('https://www.zhihu.com/');
  await page.wait(2);
  const data = await page.evaluate(`
    (async () => {
      try {
        const r = await fetch('https://www.zhihu.com/api/v4/me?include=url_token', { credentials: 'include' });
        if (!r.ok) return { __httpError: r.status };
        return await r.json();
      } catch (e) {
        return { __exception: String(e && e.message || e) };
      }
    })()
  `);
  if (data?.__exception) {
    throw new CommandExecutionError(`Zhihu whoami failed: ${data.__exception}`);
  }
  if (!data || data.__httpError) {
    const status = data?.__httpError;
    if (status === 401 || status === 403) {
      throw new AuthRequiredError('www.zhihu.com', `Zhihu /api/v4/me returned HTTP ${status} — anonymous`);
    }
    throw new CommandExecutionError(`Zhihu identity probe failed (HTTP ${status ?? 'unknown'})`);
  }
  if (!data.url_token) {
    throw new AuthRequiredError('www.zhihu.com', 'Zhihu /api/v4/me returned no url_token — anonymous session');
  }
  return {
    url_token: String(data.url_token),
    name: String(data.name ?? ''),
    uid: String(data.uid ?? data.id ?? ''),
  };
}

View on GitHub (pinned to 49907e53dc)