jackwener/OpenCLI · warning · EmptyResultError

zhihu answer-comments

Error message

zhihu answer-comments

What it means

If the answer page loads but zero root comments come back, the command throws an EmptyResultError naming the command and the answer id. The library treats 'no comments' as a distinct outcome so callers can distinguish it from failures. Note this throws even when zero comments is legitimately correct — the answer simply has no comments yet.

Source

Thrown at clis/zhihu/answer-comments.js:72

        }

        const { answerId } = target;
        try {
            await page.goto(`https://www.zhihu.com/answer/${answerId}`);
        } catch (err) {
            throw new CommandExecutionError(
                `Failed to open Zhihu answer ${answerId}: ${err instanceof Error ? err.message : String(err)}`,
                'Open the answer URL in Chrome and retry after the page is reachable.',
            );
        }
        const currentQuestionId = page.getCurrentUrl
            ? extractQuestionIdFromAnswerUrl(await page.getCurrentUrl().catch(() => ''))
            : '';
        const questionId = target.questionId || currentQuestionId;

        const roots = await fetchRootComments(page, answerId, order, topLevelLimit);
        if (roots.length === 0) {
            throw new EmptyResultError('zhihu answer-comments', `No comments found for answer ${answerId}.`);
        }
        const repliesByRoot = await fetchRepliesByRoot(page, roots, repliesLimit);
        return buildCommentRows(roots, repliesByRoot, { answerId, questionId });
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Confirm in a browser that the answer actually has comments
  2. Try another answer id to verify the command works
  3. Handle EmptyResultError as 'no data' rather than a failure in batch scripts
  4. Retry while logged in if Zhihu serves empty payloads to anonymous sessions

Example fix

// before
const rows = await runZhihuAnswerComments(id);
// after
let rows;
try { rows = await runZhihuAnswerComments(id); }
catch (err) {
  if (err.name === 'EmptyResultError') rows = [];
  else throw err;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check the page in a browser or via a quick fetch that comments exist before batch-running

Try / catch

try { rows = await answerComments(id); }
catch (err) {
  if (err.constructor.name === 'EmptyResultError') rows = []; // no comments on this answer
  else throw err;
}

Prevention

When it happens

Trigger: A brand-new or very obscure answer with no comments; comments disabled for the answer/question; all comments removed by moderation so fetchRootComments returns []; anti-bot serving an empty comments payload while the page loads fine.

Common situations: Querying freshly posted answers; scraping answers in locked/closed questions; logged-out sessions getting empty comment lists; region-restricted content.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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