{"record":{"id":"7146414b60e1286b","repo":"jackwener/OpenCLI","slug":"twitter-profile-response-payload-is-malformed","errorCode":null,"errorMessage":"Twitter profile response payload is malformed","messagePattern":"Twitter profile response payload is malformed","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/twitter/profile.js","lineNumber":168,"sourceCode":"            httpStatus: resp.status,\n            error: 'HTTP ' + resp.status,\n            hint: 'User may not exist, auth may be required, or queryId expired'\n          };\n        }\n        let d;\n        try {\n          d = await resp.json();\n        } catch (error) {\n          return {ok: false, error: 'Twitter profile response was not JSON: ' + String(error && error.message || error)};\n        }\n\n        const result = d.data?.user?.result;\n        if (!result) return {ok: false, notFound: true, error: 'User @' + screenName + ' not found'};\n        return {ok: true, result};\n      }\n    `));\n        if (!isPlainObject(rawResult)) {\n            throw new CommandExecutionError('Twitter profile response payload is malformed');\n        }\n        if (!rawResult.ok) {\n            // For HTTP errors, use fork's rich code mapping (429/401/403/404/5xx differentiation\n            // from describeTwitterApiError); fall back to the plain message for non-HTTP failures\n            // (fetch threw, JSON parse failed, payload malformed).\n            const message = typeof rawResult.httpStatus === 'number'\n                ? describeTwitterApiError('UserByScreenName', rawResult.httpStatus, rawResult.hint)\n                : rawResult.error + (rawResult.hint ? ` (${rawResult.hint})` : '');\n            if (rawResult.auth) {\n                throw new AuthRequiredError('x.com', message);\n            }\n            if (rawResult.notFound) {\n                throw new EmptyResultError('twitter profile', message);\n            }\n            throw new CommandExecutionError(message);\n        }\n        return mapTwitterProfileResult(rawResult.result, username);\n    }","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/twitter/profile.js#L150-L186","documentation":"CommandExecutionError thrown when the result of the in-page fetch of the UserByScreenName GraphQL endpoint is not a plain object. The library expects the page.evaluate payload to be an object like {ok, result, ...}; anything else (null, array, primitive) means the response pipeline broke rather than a normal API outcome.","triggerScenarios":"page.evaluate returns a non-object — e.g. the injected script threw and returned undefined, the fetch wrapper was overridden in page context, or unwrapBrowserResult yielded null because the browser returned a wrapped/unexpected value.","commonSituations":"x.com changed its GraphQL response envelope (breaking change); an extension or anti-bot script corrupts window state; the evaluate string is built with older quoting/serialization; headless environment blocks fetch so the promise resolves to undefined.","solutions":["Re-run the command once to rule out a transient page-context failure","Check for x.com API/GraphQL envelope changes and update the library version","Disable browser extensions or anti-bot interference in the automation profile","Wrap the call in try-catch for CommandExecutionError and fall back to re-initializing the page before retrying"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isPlainObject(v) {\n  return v !== null && typeof v === 'object' && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  const profile = await cli.run('twitter profile', { username });\n} catch (e) {\n  if (e instanceof CommandExecutionError && /malformed/.test(e.message)) {\n    await reopenPage(); // fresh page context\n    return cli.run('twitter profile', { username });\n  }\n  throw e;\n}","preventionTips":["Keep the library updated against x.com GraphQL envelope changes","Avoid extensions that patch fetch/JSON in the automation profile","Validate evaluate return shapes at the boundaries in your own wrappers","Log raw evaluate results when debugging payload issues"],"tags":["malformed-response","graphql","browser-automation"],"backgroundTag":"malformed-api-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}