{"record":{"id":"7c1b436a8077d331","repo":"jackwener/OpenCLI","slug":"bilibili-user-search-returned-malformed-mid-for-7c1b43","errorCode":null,"errorMessage":"Bilibili user search returned malformed mid for ${input}","messagePattern":"Bilibili user search returned malformed mid for (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/utils.js","lineNumber":297,"sourceCode":"export 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":279,"sourceCodeEnd":303,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/utils.js#L279-L303","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry the search; transient partial results may resolve","Try resolving by UID directly (uid command) instead of by username","Inspect the search response and update the mid-extraction logic in clis/bilibili/utils.js if the schema changed","Pick a different match from results instead of only results[0] if the schema now carries multiple entry shapes"],"exampleFix":"// before\nconst mid = String(results[0]?.mid ?? '').trim();\n// after\nconst hit = results.find(r => String(r?.mid ?? '').trim());\nconst mid = hit ? String(hit.mid).trim() : '';\nif (!mid) throw new CommandExecutionError(`Bilibili user search returned malformed mid for ${input}`);","handlingStrategy":"type-guard","validationCode":"// verify the input resolves on bilibili.com search in a browser first if repeated failures occur","typeGuard":"function hasMid(result) {\n  return !!result && typeof result === 'object' && typeof String(result.mid ?? '').trim() === 'string' && String(result.mid ?? '').trim() !== '';\n}","tryCatchPattern":"try { const mid = await resolveUid(input); }\ncatch (e) {\n  if (e instanceof CommandExecutionError && e.message.includes('malformed mid')) console.error('Top search hit has no mid; try UID lookup');\n  else throw e;\n}","preventionTips":["Keep the mid-extraction logic resilient (search all results, not just [0])","Handle new result entry types from Bilibili schema updates","Fall back to UID-based resolution when mid is missing","Log results[0] on failure to detect schema drift early"],"tags":["api","malformed-response","bilibili"],"backgroundTag":"api-response-schema-changed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}