{"record":{"id":"e55db9d8d9acb4b3","repo":"jackwener/OpenCLI","slug":"index-must-be-a-positive-integer-e55db9","errorCode":null,"errorMessage":"index must be a positive integer","messagePattern":"index must be a positive integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/save.js","lineNumber":23,"sourceCode":"    access: 'write',\n    description: 'Save (bookmark) an Instagram post',\n    domain: 'www.instagram.com',\n    args: [\n        {\n            name: 'username',\n            required: true,\n            positional: true,\n            help: 'Username of the post author',\n        },\n        { name: 'index', type: 'int', default: 1, help: 'Post index (1 = most recent)' },\n    ],\n    columns: ['status', 'user', 'post'],\n    pipeline: [\n        { navigate: 'https://www.instagram.com' },\n        { evaluate: `(async () => {\n  const username = \\${{ args.username | 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');","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/save.js#L5-L41","documentation":"The `save` command accepts a 1-based `--index` argument identifying which post of a user's feed to bookmark. Inside the browser pipeline, the raw args.index value is interpolated directly and decremented to a 0-based `idx`; if the resulting value is not an integer or is negative, the script throws this error before any network calls. It is a guard against non-numeric or sub-1 (e.g. 0 or negative) post positions, since the count parameter in the feed API request would otherwise be invalid.","triggerScenarios":"Running the instagram save command with an `--index` value that, after the `args.index - 1` subtraction, is not a non-negative integer: passing 0 or a negative number, passing a non-integer like 1.5 (when the arg is not coerced to int), or passing a value that interpolates as NaN/undefined (e.g. missing or malformed argument).","commonSituations":"A developer thinks the index is 0-based and passes `--index 0` to target the first (most recent) post; a wrapper script forwards an empty or non-numeric variable as the index; an automation supplies a float from a computation; or the arg type 'int' default fails and `undefined - 1` becomes NaN.","solutions":["Pass a whole-number index >= 1 (1 = the most recent post), e.g. `--index 3` for the third newest post.","If you meant the first post, omit `--index` entirely — it defaults to 1.","Check the calling script/wrapper for empty or non-numeric values being forwarded as the index and coerce/validate them before invoking the command."],"exampleFix":"// before (0-based intent)\ninstagram save someuser --index 0\n\n// after (1-based: 1 = most recent)\ninstagram save someuser --index 1","handlingStrategy":"validation","validationCode":"const idx = Number(args.index);\nif (!Number.isInteger(idx) || idx < 1) {\n  throw new Error(`--index must be a whole number >= 1 (1 = most recent post), got: ${args.index}`);\n}","typeGuard":"function isValidIndex(v) {\n  return typeof v === 'number' && Number.isInteger(v) && v >= 1;\n}","tryCatchPattern":"try {\n  await run(['instagram', 'save', username, '--index', String(i)]);\n} catch (e) {\n  if (String(e.message).includes('index must be a positive integer')) {\n    console.error('Use a 1-based index (1 = most recent post)');\n  }\n  throw e;\n}","preventionTips":["Always treat --index as 1-based: 1 is the most recent post, never pass 0.","Coerce and validate user-supplied index in wrapper scripts before invoking the command.","Omit --index to accept the default of 1 when you want the newest post."],"tags":["validation","argument-error","instagram"],"backgroundTag":"invalid-argument-value","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}