jackwener/OpenCLI · error · CliError

COMMAND_EXEC

COMMAND_EXEC

Error message

${apiResult?.message || 'Failed to create comment'}

What it means

After submitting the comment through Zhihu's API inside the page context, the CLI wraps any non-ok result in CliError('COMMAND_EXEC', ...), surfacing the API's error message (or 'Failed to create comment' if none). The in-page call already extracts resp.error.message or detects a missing created comment id, so this error is the normalized failure of the comment-create API itself.

Source

Thrown at clis/zhihu/comment.js:48

        const apiResult = await page.evaluate(`(async () => {
            var targetKind = ${JSON.stringify(target.kind)};
            var targetId = ${JSON.stringify(target.id)};
            var content = ${JSON.stringify(payload)};
            var resourceType = targetKind === 'answer' ? 'answers' : 'articles';
            var url = 'https://www.zhihu.com/api/v4/' + resourceType + '/' + targetId + '/comments';
            var resp = await fetch(url, {
                method: 'POST',
                credentials: 'include',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ content: content }),
            });
            var data = await resp.json();
            if (!resp.ok) return { ok: false, status: resp.status, message: data.error ? data.error.message : 'unknown error' };
            if (!data || !data.id) return { ok: false, status: resp.status, message: 'Comment API response did not include a created comment id' };
            return { ok: true, id: data.id, url: data.url };
        })()`);
        if (!apiResult?.ok) {
            throw new CliError('COMMAND_EXEC', apiResult?.message || 'Failed to create comment');
        }
        return buildResultRow(`Commented on ${target.kind} ${target.id}`, target.kind, rawTarget, 'created', {
            author_identity: authorIdentity,
            created_url: apiResult.url || '',
        });
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Read the embedded message: if it indicates auth (401/403), re-login to zhihu.com in the browser session and retry.
  2. Wait and retry if the message suggests rate limiting/anti-spam (429); reduce comment frequency.
  3. Verify the target answer/article still accepts comments (not closed or restricted).
  4. If the message is 'unknown error' or the id-missing variant, capture resp.status and inspect the API response for a Zhihu schema change and update the in-page parsing.

Example fix

// before: unhandled
catch (e) { console.error(e.message); }
// after: branch on the normalized error
try {
  await zhihuComment(page, kwargs);
} catch (e) {
  if (e.code === 'COMMAND_EXEC' && /401|403|log ?in/i.test(e.message)) {
    console.error('Zhihu auth needed — log in and retry');
  } else throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!execute) return { status: 'dry-run' }; // requireExecute guard; also pre-check auth with zhihu me

Type guard

function isApiOk(r) { return r != null && r.ok === true && typeof r.id === 'string'; }

Try / catch

try {
  await zhihuComment(page, kwargs);
} catch (e) {
  if (e.code === 'COMMAND_EXEC') {
    if (/401|403|login/i.test(e.message)) return reloginAndRetry();
    if (/429|frequen|rate/i.test(e.message)) return backoffAndRetry();
  }
  throw e;
}

Prevention

When it happens

Trigger: The Zhihu comment API returns a non-2xx status (e.g. 401 for expired auth, 403 for banned/rate-limited, 429), or returns 200 with a body lacking data.id/data.url; the in-page promise resolves with { ok:false } and the CLI throws.

Common situations: Posting a comment while not logged in or after session expiry; hitting Zhihu's anti-spam/rate limiting after repeated comments; commenting on a closed or restricted answer/article; Zhihu changing the response schema so `data.id` is absent.

Related errors


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