{"record":{"id":"805e8f12e028ee7f","repo":"jackwener/OpenCLI","slug":"bilibili-user-search-returned-malformed-result-for","errorCode":null,"errorMessage":"Bilibili user search returned malformed result for ${input}","messagePattern":"Bilibili user search returned malformed result for (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/utils.js","lineNumber":288,"sourceCode":"  `);\n}\nexport async function getSelfUid(page) {\n    const nav = await getNavData(page);\n    const mid = nav?.data?.mid;\n    if (!mid)\n        throw new AuthRequiredError('bilibili.com');\n    return String(mid);\n}\nexport async function resolveUid(page, input) {\n    if (/^\\d+$/.test(input))\n        return input;\n    // Search for user by name\n    const payload = await apiGet(page, '/x/web-interface/wbi/search/type', {\n        params: { search_type: 'bili_user', keyword: input },\n        signed: true,\n    });\n    if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !payload.data || typeof payload.data !== 'object' || Array.isArray(payload.data) || !Object.hasOwn(payload.data, 'result')) {\n        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);\n    }\n    const results = payload.data.result;\n    if (!Array.isArray(results)) {\n        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);\n    }\n    if (results.length > 0) {\n        const mid = String(results[0]?.mid ?? '').trim();\n        if (!mid) {\n            throw new CommandExecutionError(`Bilibili user search returned malformed mid for ${input}`);\n        }\n        return mid;\n    }\n    throw new EmptyResultError(`bilibili user search: ${input}`, 'User may not exist or username may have changed.');\n}\n","sourceCodeStart":270,"sourceCodeEnd":303,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/utils.js#L270-L303","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-run the search — transient degraded responses often succeed on retry.","Print the raw payload to see what Bilibili actually returned for that keyword (check payload.code/message for the real cause).","If you have the numeric mid, pass that instead of a username — it skips the search entirely (resolveUid returns digits immediately).","Try a more exact username; empty or odd results can come back with unusual data shapes."],"exampleFix":"// before\nconst mid = await resolveUid(page, 'someuser'); // may throw on odd shapes\n// after\nconst mid = /^\\d+$/.test(input)\n  ? input\n  : await resolveUid(page, input); // or catch & inspect raw payload","handlingStrategy":"try-catch","validationCode":"// prefer numeric mid when known — skips search entirely\nconst mid = /^\\d+$/.test(input) ? input : null;\nif (!mid) console.warn('username search may return unusual shapes');","typeGuard":"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); }","tryCatchPattern":"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; }","preventionTips":["Pass numeric mid directly when available to avoid the search path.","Retry once on malformed search responses before giving up.","Log raw payloads to distinguish risk-control responses from schema changes.","Use exact usernames; ambiguous keywords increase odd-response likelihood."],"tags":["api","validation","bilibili","search","schema"],"backgroundTag":"malformed-api-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}