{"record":{"id":"f0f577cf5b5210aa","repo":"jackwener/OpenCLI","slug":"bilibili-label-api-returned-a-malformed-payload","errorCode":null,"errorMessage":"Bilibili ${label} API returned a malformed payload","messagePattern":"Bilibili (.+?) API returned a malformed payload","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/comments.js","lineNumber":38,"sourceCode":"        throw new ArgumentError(`bilibili comments limit must be an integer between 1 and ${MAX_LIMIT}`);\n    }\n    return limit;\n}\n\nfunction parseParent(value) {\n    if (value == null) {\n        return null;\n    }\n    const parent = Number(value);\n    if (!Number.isInteger(parent) || parent <= 0) {\n        throw new ArgumentError('bilibili comments parent must be a positive integer rpid');\n    }\n    return parent;\n}\n\nfunction requireOkPayload(payload, label) {\n    if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !Object.hasOwn(payload, 'code')) {\n        throw new CommandExecutionError(`Bilibili ${label} API returned a malformed payload`);\n    }\n    if (payload.code !== 0) {\n        const message = payload.message ?? 'unknown error';\n        if (isAuthLikeBilibiliError(payload.code, message)) {\n            throw new AuthRequiredError('bilibili.com', `Bilibili ${label} API requires login or permission: ${message} (${payload.code})`);\n        }\n        throw new CommandExecutionError(`Bilibili ${label} API failed: ${message} (${payload.code})`);\n    }\n    return payload.data;\n}\n\nfunction requireReplies(data, label) {\n    if (!data || typeof data !== 'object' || Array.isArray(data)) {\n        throw new CommandExecutionError(`Bilibili ${label} API returned malformed data`);\n    }\n    if (!Object.hasOwn(data, 'replies')) {\n        throw new CommandExecutionError(`Bilibili ${label} API did not return replies`);\n    }","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/comments.js#L20-L56","documentation":"CommandExecutionError thrown by requireOkPayload when the Bilibili API response is not an object with a numeric 'code' field — i.e. the response envelope itself is missing or unrecognizable. This library expects Bilibili's standard {code, message, data} envelope on every /x/web-interface/view, /x/v2/reply/main, or /x/v2/reply/reply call.","triggerScenarios":"The view/reply API returned HTML (e.g. a login page, risk-control intercept, or CDN error page), an empty body, an array, or JSON without a 'code' key instead of the standard envelope.","commonSituations":"Bilibili risk control (captcha/anti-bot) intercepting the cookie-based request; expired session causing an HTML redirect; network middleboxes or proxies returning error pages; Bilibili API contract changes.","solutions":["Re-login / refresh the browser cookie session (the CLI uses Strategy.COOKIE) and retry","Fetch the URL manually in the page to inspect what the API actually returned","Check for Bilibili risk-control or maintenance, wait and retry later","Update the library in case Bilibili changed the response envelope"],"exampleFix":"// before (raw fetch assuming envelope)\nconst payload = JSON.parse(await res.text());\n// after\nconst body = JSON.parse(await res.text());\nif (!body || typeof body !== 'object' || !('code' in body)) throw new Error('unexpected bilibili response: ' + await res.clone().text().catch(() => 'n/a'));","handlingStrategy":"retry","validationCode":"null","typeGuard":"function isBiliEnvelope(x) { return !!x && typeof x === 'object' && !Array.isArray(x) && Object.hasOwn(x, 'code'); }","tryCatchPattern":"try { const rows = await run(['bilibili','comments',bvid]); } catch (e) { if (/malformed payload/.test(e.message)) { await refreshSession(); return retryWithBackoff(fn, 2); } throw e; }","preventionTips":["Keep the cookie session fresh; re-login when you see envelope-level failures","Use a stable network (avoid proxies that inject error pages)","Pin/monitor library versions for Bilibili API contract changes"],"tags":["network","api-response","schema-validation"],"backgroundTag":"api-malformed-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}