jackwener/OpenCLI · error · CommandExecutionError

Bilibili user search returned malformed mid for ${input}

Error message

Bilibili user search returned malformed mid for ${input}

What it means

After resolveUid gets a non-empty result array from Bilibili user search, it reads results[0].mid. This CommandExecutionError fires when the top result exists but its mid field is missing/empty after string-trim, i.e. a malformed first search hit.

Source

Thrown at clis/bilibili/utils.js:297

export async function resolveUid(page, input) {
    if (/^\d+$/.test(input))
        return input;
    // Search for user by name
    const payload = await apiGet(page, '/x/web-interface/wbi/search/type', {
        params: { search_type: 'bili_user', keyword: input },
        signed: true,
    });
    if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !payload.data || typeof payload.data !== 'object' || Array.isArray(payload.data) || !Object.hasOwn(payload.data, 'result')) {
        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);
    }
    const results = payload.data.result;
    if (!Array.isArray(results)) {
        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);
    }
    if (results.length > 0) {
        const mid = String(results[0]?.mid ?? '').trim();
        if (!mid) {
            throw new CommandExecutionError(`Bilibili user search returned malformed mid for ${input}`);
        }
        return mid;
    }
    throw new EmptyResultError(`bilibili user search: ${input}`, 'User may not exist or username may have changed.');
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Retry the search; transient partial results may resolve
  2. Try resolving by UID directly (uid command) instead of by username
  3. Inspect the search response and update the mid-extraction logic in clis/bilibili/utils.js if the schema changed
  4. Pick a different match from results instead of only results[0] if the schema now carries multiple entry shapes

Example fix

// before
const mid = String(results[0]?.mid ?? '').trim();
// after
const hit = results.find(r => String(r?.mid ?? '').trim());
const mid = hit ? String(hit.mid).trim() : '';
if (!mid) throw new CommandExecutionError(`Bilibili user search returned malformed mid for ${input}`);
Defensive patterns

Strategy: type-guard

Validate before calling

// verify the input resolves on bilibili.com search in a browser first if repeated failures occur

Type guard

function hasMid(result) {
  return !!result && typeof result === 'object' && typeof String(result.mid ?? '').trim() === 'string' && String(result.mid ?? '').trim() !== '';
}

Try / catch

try { const mid = await resolveUid(input); }
catch (e) {
  if (e instanceof CommandExecutionError && e.message.includes('malformed mid')) console.error('Top search hit has no mid; try UID lookup');
  else throw e;
}

Prevention

When it happens

Trigger: Bilibili user search returns results[0] as an object lacking 'mid' (or mid is null/empty string), e.g. for special/renamed/protected accounts that appear in search but expose no mid.

Common situations: Bilibili schema drift adding new entry types to search results; searching usernames that resolve to composite/promotional cards without a mid; partial responses during API instability.

Understand the failure class

Related errors


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