{"record":{"id":"f2497b6fb752c498","repo":"jackwener/OpenCLI","slug":"browser-session-required-for-instagram-post","errorCode":null,"errorMessage":"Browser session required for instagram post","messagePattern":"Browser session required for instagram post","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/instagram/post.js","lineNumber":84,"sourceCode":"      const sharingVisible = /sharing/.test(visibleText);\n      const shared = /post shared|your post has been shared|已分享|已发布/.test(visibleText)\n        || /\\\\/p\\\\//.test(url);\n      const failed = !shared && !sharingVisible && (\n        /couldn['’]t be shared|could not be shared|failed to share|share failed|无法分享|分享失败/.test(visibleText)\n        || (/something went wrong/.test(visibleText) && /try again/.test(visibleText))\n      );\n      const composerOpen = dialogs.some((dialog) =>\n        !!dialog.querySelector('textarea, [contenteditable=\"true\"], input[type=\"file\"]')\n        || /write a caption|add location|advanced settings|select from computer|crop|filters|adjustments|sharing/.test((dialog.textContent || '').toLowerCase())\n      );\n      const settled = !shared && !composerOpen && !/sharing/.test(visibleText);\n      return { ok: shared, failed, settled, url: /\\\\/p\\\\//.test(url) ? url : '' };\n    })()\n  `;\n}\nfunction requirePage(page) {\n    if (!page)\n        throw new CommandExecutionError('Browser session required for instagram post');\n    return page;\n}\nfunction validateMixedMediaItems(inputs) {\n    if (!inputs.length) {\n        throw new ArgumentError('Argument \"media\" is required.', 'Provide --media /path/to/file.jpg or --media /path/a.jpg,/path/b.mp4');\n    }\n    if (inputs.length > MAX_MEDIA_ITEMS) {\n        throw new ArgumentError(`Too many media items: ${inputs.length}`, `Instagram carousel posts support at most ${MAX_MEDIA_ITEMS} items`);\n    }\n    const items = inputs.map((input) => {\n        const resolved = path.resolve(String(input || '').trim());\n        if (!resolved) {\n            throw new ArgumentError('Media path cannot be empty');\n        }\n        if (!fs.existsSync(resolved)) {\n            throw new ArgumentError(`Media file not found: ${resolved}`);\n        }\n        const ext = path.extname(resolved).toLowerCase();","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/post.js#L66-L102","documentation":"requirePage throws this CommandExecutionError when the `page` argument passed into the instagram post command is null/undefined. All instagram UI-strategy commands require an active browser page (a logged-in browser session) because they drive the real instagram.com DOM. The library throws early instead of failing obscurely later during page.goto/evaluate.","triggerScenarios":"Calling the instagram post command without first starting/providing a browser session, or passing null/undefined for the page handle — e.g. running the command headless-without-browser, or after a browser session was closed and the stale page handle is null.","commonSituations":"Developers forget to run the browser/session-open command before posting; a script reuses a page variable from a session that already exited; running in an environment where browser:true commands were invoked without a browser backend; the previous step in a pipeline failed silently and returned no page.","solutions":["Open a browser session for instagram before posting (the command declares browser: true — supply the page it creates).","Verify the page object is non-null before invoking the command in your own orchestration code.","If a previous session was closed, restart the browser and obtain a fresh page handle instead of reusing the old variable.","Check earlier pipeline steps for swallowed errors that left the page undefined.","Ensure your environment actually supports the browser backend (installed/launchable browser) so a page can be created."],"exampleFix":"// before\nlet page; // never assigned or session closed\nawait postToInstagram(page, { media: 'a.jpg' });\n// after\nconst page = await openBrowserSession('instagram');\nif (!page) throw new Error('Failed to open browser session');\nawait postToInstagram(page, { media: 'a.jpg' });","handlingStrategy":"type-guard","validationCode":"// Before invoking the command\nif (!page) throw new Error('Open a browser session for instagram first (command requires browser: true)');\nif (typeof page.goto !== 'function' || typeof page.evaluate !== 'function') {\n  throw new Error('page is not a valid browser page handle');\n}","typeGuard":"function hasBrowserPage(page) {\n  return !!page && typeof page === 'object' && typeof page.goto === 'function' && typeof page.evaluate === 'function' && typeof page.wait === 'function';\n}","tryCatchPattern":"try {\n  await cli.run('instagram post', { media: 'a.jpg' });\n} catch (e) {\n  if (e instanceof CommandExecutionError && e.message === 'Browser session required for instagram post') {\n    await cli.run('open browser', { site: 'instagram' }); // re-establish session, then retry once\n    await cli.run('instagram post', { media: 'a.jpg' });\n  } else throw e;\n}","preventionTips":["Always run the browser/session-open step before any instagram browser command","Never cache page handles across session restarts","Check for null page after session-open calls in orchestration code","Fail loudly in your own pipeline when an upstream step returns no page","Keep the browser process alive for the duration of the command"],"tags":["instagram","browser-session","null-check","prerequisite-missing"],"backgroundTag":"missing-browser-session","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}