{"record":{"id":"16f5d7f9292aa65c","repo":"jackwener/OpenCLI","slug":"could-not-resolve-username","errorCode":null,"errorMessage":"Could not resolve @${username}","messagePattern":"Could not resolve @(.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/twitter/user-timeline.js","lineNumber":207,"sourceCode":"    const ct0 = cookies.find((cookie) => cookie.name === 'ct0')?.value || null;\n    if (!ct0) throw new AuthRequiredError('x.com', 'Not logged into x.com (no ct0 cookie)');\n\n    const userTweetsOperation = await resolveTwitterOperationMetadata(page, 'UserTweets', USER_TWEETS_OPERATION);\n    const userByScreenNameOperation = await resolveTwitterOperationMetadata(page, 'UserByScreenName', USER_BY_SCREEN_NAME_OPERATION);\n    const headers = JSON.stringify({\n        Authorization: `Bearer ${decodeURIComponent(TWITTER_BEARER_TOKEN)}`,\n        'X-Csrf-Token': ct0,\n        'X-Twitter-Auth-Type': 'OAuth2Session',\n        'X-Twitter-Active-User': 'yes',\n    });\n    const userByScreenNameUrl = buildUserByScreenNameUrl(userByScreenNameOperation, username);\n    const userId = unwrapBrowserResult(await page.evaluate(`async () => {\n        const resp = await fetch(${JSON.stringify(userByScreenNameUrl)}, { headers: ${headers}, credentials: 'include' });\n        if (!resp.ok) return null;\n        const data = await resp.json();\n        return data?.data?.user?.result?.rest_id || null;\n    }`));\n    if (!userId) throw new CommandExecutionError(`Could not resolve @${username}`);\n    return { username, userId, headers, userTweetsOperation };\n}\n\nexport async function fetchUserTimelinePage(page, context, cursor, count) {\n    const url = buildUserTweetsUrl(context.userTweetsOperation, context.userId, count, cursor);\n    return normalizeTwitterGraphqlPayload(await page.evaluate(`async () => {\n        const response = await fetch(${JSON.stringify(url)}, { headers: ${context.headers}, credentials: 'include' });\n        return response.ok ? await response.json() : { error: response.status };\n    }`));\n}\n","sourceCodeStart":189,"sourceCodeEnd":218,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/twitter/user-timeline.js#L189-L218","documentation":"After building UserByScreenName GraphQL headers, resolveUserTimelineContext evaluates a fetch in the page to map a @handle to its numeric rest_id. If the response is not ok or the JSON lacks data.user.result.rest_id, it throws CommandExecutionError `Could not resolve @<username>`. This means Twitter did not return a user for that screen name under the current session.","triggerScenarios":"Passing a username that does not exist, was renamed/deactivated/suspended, is misspelled (including a leading '@' if the URL builder doesn't strip it), or calling while the GraphQL request is rejected (rate limited, auth issues) so resp.ok is false and userId is null.","commonSituations":"Typo in the handle; account deleted or renamed since it was saved; querying a protected account while not following it; X rate-limiting the UserByScreenName endpoint; stale operation metadata (queryId rotation) causing non-ok responses.","solutions":["Double-check the handle: confirm the account exists by visiting https://x.com/<username> in the same logged-in browser.","Retry later if X is rate-limiting or having an incident; verify with a browser request that UserByScreenName returns 200.","Strip any leading '@' or trailing whitespace from the username before calling.","Refresh the UserByScreenName operation metadata (queryId) if X rotated it, and re-login if the session degraded."],"exampleFix":"// before\nawait fetchUserTimeline(page, 'elonmusk '); // trailing space\n// after\nconst username = raw.trim().replace(/^@/, '');\nawait fetchUserTimeline(page, username);","handlingStrategy":"validation","validationCode":"const handle = raw.trim().replace(/^@/, '');\nif (!/^[A-Za-z0-9_]{1,15}$/.test(handle)) {\n  throw new Error(`Not a valid X handle: ${raw}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await fetchUserTimeline(page, handle);\n} catch (err) {\n  if (/Could not resolve @/.test(err.message)) {\n    console.error(`Account @${handle} not found, renamed, or suspended; verify at https://x.com/${handle}`);\n    return null;\n  }\n  throw err;\n}","preventionTips":["Normalize handles: trim, strip leading '@', validate against /^[A-Za-z0-9_]{1,15}$/.","Confirm the account exists before batch runs (visit x.com/<handle> or one lookup).","Back off and retry on rate-limit-like failures; refresh operation queryIds when X rotates them."],"tags":["api","username-resolution","graphql","twitter"],"backgroundTag":"resource-not-found","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}