{"record":{"id":"6748d69afe28bcaf","repo":"jackwener/OpenCLI","slug":"bilibili-relation-query-returned-a-malformed-attri","errorCode":null,"errorMessage":"Bilibili relation query returned a malformed attribute","messagePattern":"Bilibili relation query returned a malformed attribute","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/relation.js","lineNumber":27,"sourceCode":"    if (!trimmed) return '';\n    const candidate = /^https?:\\/\\//i.test(trimmed) ? trimmed : `https://${trimmed}`;\n    let parsed;\n    try {\n        parsed = new URL(candidate);\n    } catch {\n        return '';\n    }\n    if (parsed.hostname.toLowerCase() !== 'space.bilibili.com') return '';\n    const match = parsed.pathname.match(/^\\/(\\d+)\\/?$/);\n    return match ? match[1] : '';\n}\n\nexport async function fetchRelationAttribute(page, mid) {\n    const payload = await fetchJson(page, `https://api.bilibili.com/x/relation?fid=${mid}`);\n    requireOkPayload(payload, 'relation query');\n    const attribute = payload?.data?.attribute;\n    if (typeof attribute !== 'number') {\n        throw new CommandExecutionError('Bilibili relation query returned a malformed attribute');\n    }\n    return attribute;\n}\n\nexport async function waitForRelation(page, mid, predicate, expectedLabel) {\n    const deadline = Date.now() + RELATION_VERIFY_TIMEOUT_MS;\n    let lastAttribute;\n    while (Date.now() <= deadline) {\n        lastAttribute = await fetchRelationAttribute(page, mid);\n        if (predicate(lastAttribute)) return lastAttribute;\n        if (typeof page.wait !== 'function') break;\n        await page.wait({ time: RELATION_VERIFY_POLL_MS / 1000 });\n    }\n    throw new CommandExecutionError(\n        `Bilibili relation modify did not verify ${expectedLabel}; last attribute=${lastAttribute}`,\n    );\n}\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/relation.js#L9-L45","documentation":"fetchRelationAttribute expects payload.data.attribute to be a number (Bilibili's relation attribute byte). After requireOkPayload passes (code===0), a missing or non-numeric attribute means the response shape is not what the library supports, so it throws this CommandExecutionError to prevent acting on garbage data.","triggerScenarios":"Bilibili returning code 0 but data lacking attribute (e.g. special account types, API schema changes, soft-deactivated accounts), or an interceptor/proxy rewriting the JSON body.","commonSituations":"Bilibili A/B-testing new relation API responses, querying suspended/deactivated users whose data payload omits attribute, or a mock/test server with an incomplete response.","solutions":["Log the raw payload when this fires to confirm the shape; if Bilibili changed the schema, update fetchRelationAttribute.","Retry — transient anomalous responses can occur; waitForRelation callers can tolerate via predicate retries.","Skip accounts whose payload lacks attribute in batch automation.","Pin/verify no proxy or response-mocking middleware is mangling the body."],"exampleFix":"// before\nconst attribute = payload?.data?.attribute;\nif (typeof attribute !== 'number') throw new CommandExecutionError('Bilibili relation query returned a malformed attribute');\n// after\nconst attribute = payload?.data?.attribute;\nif (typeof attribute !== 'number') {\n  console.error('relation payload:', JSON.stringify(payload));\n  throw new CommandExecutionError('Bilibili relation query returned a malformed attribute');\n}","handlingStrategy":"type-guard","validationCode":"const payload = await fetchJson(page, `https://api.bilibili.com/x/relation?fid=${mid}`);\nif (payload?.code !== 0 || typeof payload?.data?.attribute !== 'number') {\n  throw new Error(`unexpected relation payload for ${mid}: ${JSON.stringify(payload).slice(0, 200)}`);\n}","typeGuard":"function hasNumericAttribute(p) {\n  return typeof p?.data?.attribute === 'number';\n}","tryCatchPattern":"try {\n  const attr = await fetchRelationAttribute(page, mid);\n} catch (e) {\n  if (String(e.message).includes('malformed attribute')) {\n    console.warn(`relation data unavailable for ${mid}; skipping`);\n    return null;\n  }\n  throw e;\n}","preventionTips":["Log raw payloads when schema assertions fail","Skip/sandbox accounts with anomalous payloads in batch jobs","Retry once — transient malformed responses can occur","Keep the parser updated if Bilibili changes the relation API shape"],"tags":["schema","api-error","bilibili"],"backgroundTag":"response-schema-mismatch","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}