{"record":{"id":"cf05a56c0f7dbf4e","repo":"jackwener/OpenCLI","slug":"post-index-idx-1-not-found","errorCode":null,"errorMessage":"Post index ${(idx + 1)} not found","messagePattern":"Post index (.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/comment.js","lineNumber":39,"sourceCode":"        { evaluate: `(async () => {\n  const username = \\${{ args.username | json }};\n  const commentText = \\${{ args.text | json }};\n  const idx = \\${{ args.index }} - 1;\n  if (!Number.isInteger(idx) || idx < 0) throw new Error('index must be a positive integer');\n  const headers = { 'X-IG-App-ID': '936619743392459' };\n  const opts = { credentials: 'include', headers };\n  async function readInstagramJson(response, label) {\n    try {\n      return await response.json();\n    } catch {\n      throw new Error(label + ' returned invalid JSON');\n    }\n  }\n  function getPostFromFeed(feed, label) {\n    if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {\n      throw new Error(label + ' returned malformed items payload');\n    }\n    if (idx >= feed.items.length) throw new Error('Post index ' + (idx + 1) + ' not found');\n    const post = feed.items[idx];\n    const pkRaw = post?.pk ?? post?.id;\n    const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');\n    if (!/^\\\\d+$/.test(pk)) throw new Error(label + ' returned malformed post row');\n    return { pk };\n  }\n  function assertOkStatus(payload, label) {\n    if (!payload || typeof payload !== 'object' || payload.status !== 'ok') {\n      throw new Error(label + ' returned no success evidence');\n    }\n  }\n\n  // web_profile_info answers HTTP 400 for business accounts; feed-by-username needs no user id. See #2234.\n  const r1 = await fetch('https://www.instagram.com/api/v1/feed/user/' + encodeURIComponent(username) + '/username/?count=' + (idx + 1), opts);\n  if (!r1.ok) throw new Error(r1.status === 404 ? 'User not found: ' + username : 'HTTP ' + r1.status + ' - make sure you are logged in to Instagram');\n  const { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');\n\n  const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/comment.js#L21-L57","documentation":"Thrown when the validated index (idx = index - 1) is greater than or equal to the number of items in the fetched feed, i.e. the user's profile has fewer posts than the requested position. The 1-based index is echoed back in the message.","triggerScenarios":"Requesting e.g. index 10 when the account has only 4 posts; fetch with count=idx+1 returns fewer items than requested because the account has fewer posts, is private, or is empty.","commonSituations":"Automating 'comment on the Nth latest post' for accounts with low post counts; new or cleaned-up accounts; querying a private account where items come back empty.","solutions":["Check the target account's post count and choose an index within range (1..postCount)","Handle the error gracefully in your automation: skip the account or clamp the index","Verify you are querying the right username — a similar/renamed account may have fewer posts","Log in with valid cookies so private accounts' items are actually returned"],"exampleFix":"// before\nconst { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');\n// after\nconst feed = await readInstagramJson(r1, 'Instagram feed-by-username');\nif (!Array.isArray(feed?.items) || feed.items.length <= idx) {\n  return [{ status: 'Skipped', user: username, reason: 'fewer than ' + (idx + 1) + ' posts available' }];\n}\nconst { pk } = getPostFromFeed(feed, 'Instagram feed-by-username');","handlingStrategy":"validation","validationCode":"const feed = await readInstagramJson(r1, 'feed');\nconst count = Array.isArray(feed?.items) ? feed.items.length : 0;\nif (idx >= count) {\n  console.warn(`Only ${count} posts available for ${username}; index ${idx + 1} skipped`);\n}","typeGuard":"function hasPostAtIndex(feed, idx) {\n  return Array.isArray(feed?.items) && idx < feed.items.length;\n}","tryCatchPattern":"try {\n  await runCommentPipeline(args);\n} catch (e) {\n  if (/Post index \\d+ not found/.test(e.message)) {\n    console.warn('Skipping account: not enough posts');\n  } else throw e;\n}","preventionTips":["Check post count before choosing an index","Default to index 1 (latest post)","Skip low-post accounts in bulk automation","Confirm the account is public so items are returned"],"tags":["instagram","range-error","argument-validation"],"backgroundTag":"index-out-of-range","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}