jackwener/OpenCLI · error · CommandExecutionError

Bilibili ${label} API returned malformed top_replies

Error message

Bilibili ${label} API returned malformed top_replies

What it means

This CommandExecutionError is thrown by requireTopReplies when the Bilibili /x/v2/reply/main API response contains a top_replies field that is present but not an array (checked after verifying the field exists). It indicates the pinned-comments payload shape deviates from what the CLI expects, so it refuses to iterate over it. It guards against API schema changes or corrupted responses.

Source

Thrown at clis/bilibili/comments.js:74

    }
    if (data.replies === null) {
        return [];
    }
    if (!Array.isArray(data.replies)) {
        throw new CommandExecutionError(`Bilibili ${label} API returned malformed replies`);
    }
    return data.replies;
}

function requireTopReplies(data, label) {
    if (!data || typeof data !== 'object' || Array.isArray(data)) {
        throw new CommandExecutionError(`Bilibili ${label} API returned malformed data`);
    }
    if (!Object.hasOwn(data, 'top_replies')) {
        throw new CommandExecutionError(`Bilibili ${label} API did not return top_replies`);
    }
    if (!Array.isArray(data.top_replies)) {
        throw new CommandExecutionError(`Bilibili ${label} API returned malformed top_replies`);
    }
    return data.top_replies;
}

function formatReplyRow(reply, index) {
    if (!reply || typeof reply !== 'object' || Array.isArray(reply)) {
        throw new CommandExecutionError(`Bilibili comments reply ${index + 1} was malformed`);
    }
    const rpid = String(reply.rpid ?? '').trim();
    if (!rpid) {
        throw new CommandExecutionError(`Bilibili comments reply ${index + 1} was missing rpid`);
    }
    const ctime = Number(reply.ctime);
    if (!Number.isFinite(ctime)) {
        throw new CommandExecutionError(`Bilibili comments reply ${index + 1} was missing ctime`);
    }
    return {
        rank: index + 1,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Drop --top and fetch normal comments to check whether only the pinned path is affected
  2. Retry later — a transient/changed upstream schema may self-correct or be fixed in a CLI update
  3. Update the CLI/library to the latest version that tracks current Bilibili reply-main schema
  4. Inspect the raw API payload (data.top_replies) to confirm the shape and file an issue if the schema changed

Example fix

// before
if (!Array.isArray(data.top_replies)) {
    throw new CommandExecutionError(`Bilibili ${label} API returned malformed top_replies`);
}
// after
// tolerate null as 'no pinned comments'
const tops = data.top_replies === null ? [] : data.top_replies;
if (!Array.isArray(tops)) {
    throw new CommandExecutionError(`Bilibili ${label} API returned malformed top_replies`);
}
Defensive patterns

Strategy: type-guard

Validate before calling

const data = payload?.data;
if (data && Array.isArray(data.top_replies)) {
    // safe to proceed with --top
}

Type guard

function isTopRepliesArray(data) {
    return !!data && typeof data === 'object' && Array.isArray(data.top_replies);
}

Try / catch

try {
    const rows = await bilibiliComments(bvid, { top: true });
} catch (err) {
    if (/malformed top_replies/.test(err.message)) {
        // fall back to non-pinned comments or skip this video
    } else throw err;
}

Prevention

When it happens

Trigger: Running 'bilibili comments <bvid> --top' where the API's data object has top_replies set to a non-array value (e.g. an object, number, or string) instead of a list of pinned comments.

Common situations: Bilibili changes the reply-main schema for pinned comments; a proxy/interceptor returns rewritten JSON; edge cases where the API emits top_replies as null-like or object forms for videos with unusual pinning state.

Understand the failure class

Related errors


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