jackwener/OpenCLI · error · CommandExecutionError
Bilibili user search returned malformed mid for @${name}
Error message
Bilibili user search returned malformed mid for @${name} What it means
@mentions are resolved to user ids (mid) via resolveUid (user search). If the search returns a value that is not a positive integer, the CLI throws CommandExecutionError; EmptyResultError (user not found) is tolerated and skipped, but other errors propagate.
Source
Thrown at clis/bilibili/comment.js:68
// Resolve bvid → aid (the reply API addresses videos by aid, as `oid`)
const view = await apiGet(page, '/x/web-interface/view', { params: { bvid } });
const viewData = requireOkPayload(view, 'view');
const oid = viewData?.aid;
if (!oid)
throw new CommandExecutionError(`Cannot resolve aid for bvid: ${bvid}`);
// Resolve @username mentions to uids. Bilibili only turns "@name" into a real
// mention — one that notifies the mentioned user — when the request carries
// at_name_to_mid; a plain-text "@name" is otherwise inert and notifies nobody.
/** @type {Record<string, number>} */
const atNameToMid = {};
for (const match of message.matchAll(/@([^\s@]+)/g)) {
const name = match[1];
if (name in atNameToMid)
continue;
try {
const mid = Number(await resolveUid(page, name));
if (!Number.isInteger(mid) || mid <= 0) {
throw new CommandExecutionError(`Bilibili user search returned malformed mid for @${name}`);
}
atNameToMid[name] = mid;
}
catch (error) {
if (!(error instanceof EmptyResultError)) {
throw error;
}
// Unresolvable @name (typo, or not a user) — leave it as plain text.
}
}
// For a reply, Bilibili needs both `root` (top-level comment) and `parent`.
// Replying to a top-level comment means root === parent.
const params = {
oid,
type: 1,
message,
plat: 1,
...(parent != nullView on GitHub (pinned to 49907e53dc)
Solutions
- Verify the @username exists via bilibili search and matches exactly
- Remove or correct the @mention in --message
- Retry after a short delay if the search API was rate-limiting
- Confirm resolveUid works standalone for the problem username
Example fix
// before --message "thanks @old_name" // after --message "thanks @current_name" // verified via search
Defensive patterns
Strategy: fallback
Validate before calling
const users = await searchUser(name);
if (!users.length) throw new Error(`@${name} not found; remove the mention`); Type guard
const isValidMid = (v) => Number.isInteger(Number(v)) && Number(v) > 0;
Try / catch
try { await commentCmd(); } catch (e) { if (String(e.message).includes('malformed mid')) { console.error('Drop or fix the @mention and retry'); } else throw e; } Prevention
- Mention only usernames verified via search
- Avoid usernames with unusual characters
- Throttle mention resolution to avoid rate limits
- Treat EmptyResultError as 'skip mention' behavior
When it happens
Trigger: A --message containing @name where the search API returns a malformed/non-numeric mid for that username — duplicate or renamed usernames, search API returning unexpected payload, or resolveUid returning empty string.
Common situations: Mentioning a username that no longer exists or was renamed; usernames with special characters confusing search; rate-limited or anti-bot response degrading the search payload.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/1ce8b28ef4e7fd51.
Report an issue: GitHub.