jackwener/OpenCLI · error · CommandExecutionError

Bilibili user search returned malformed result for ${input}

Error message

Bilibili user search returned malformed result for ${input}

What it means

resolveUid resolves a username to a mid via the signed /x/web-interface/wbi/search/type endpoint (search_type=bili_user). The library expects payload.data.result to exist and be an array; if the response shape deviates, it fails closed because continuing could silently resolve to the wrong user.

Source

Thrown at clis/bilibili/utils.js:288

  `);
}
export async function getSelfUid(page) {
    const nav = await getNavData(page);
    const mid = nav?.data?.mid;
    if (!mid)
        throw new AuthRequiredError('bilibili.com');
    return String(mid);
}
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. Re-run the search — transient degraded responses often succeed on retry.
  2. Print the raw payload to see what Bilibili actually returned for that keyword (check payload.code/message for the real cause).
  3. If you have the numeric mid, pass that instead of a username — it skips the search entirely (resolveUid returns digits immediately).
  4. Try a more exact username; empty or odd results can come back with unusual data shapes.

Example fix

// before
const mid = await resolveUid(page, 'someuser'); // may throw on odd shapes
// after
const mid = /^\d+$/.test(input)
  ? input
  : await resolveUid(page, input); // or catch & inspect raw payload
Defensive patterns

Strategy: try-catch

Validate before calling

// prefer numeric mid when known — skips search entirely
const mid = /^\d+$/.test(input) ? input : null;
if (!mid) console.warn('username search may return unusual shapes');

Type guard

function isSearchResult(p){ return !!p && typeof p==='object' && !Array.isArray(p) && p.data && typeof p.data==='object' && !Array.isArray(p.data) && Array.isArray(p.data.result); }

Try / catch

try { const uid = await resolveUid(page, input); } catch (e) { if (/malformed result/.test(e.message)) { const payload = await rawSearchOnce(page, input); logRaw(payload); /* inspect code/message, retry or ask for numeric mid */ } throw e; }

Prevention

When it happens

Trigger: payload is null/not an object/an array, payload.data is missing or not an object, payload.data lacks a `result` key, or payload.data.result is not an array — while searching for a user by name.

Common situations: Bilibili returning an error body whose data lacks `result` for the given keyword (some failures put info in data instead); risk-control responses with altered shape; schema changes to the search API; WBI signature accepted but response degraded.

Understand the failure class

Related errors


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