jackwener/OpenCLI · error · CommandExecutionError

Browser session required

Error message

Browser session required

What it means

The reddit `save` CLI command requires an active browser session (a logged-in Playwright-style `page`) to operate on reddit.com. When the `func` receives no page object it throws this CommandExecutionError instead of silently failing on page.goto. It means the command was invoked outside a browser session context.

Source

Thrown at clis/reddit/save.js:18

import { CommandExecutionError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';
cli({
    site: 'reddit',
    name: 'save',
    access: 'write',
    description: 'Save or unsave a Reddit post',
    domain: 'reddit.com',
    strategy: Strategy.COOKIE,
    browser: true,
    args: [
        { name: 'post-id', type: 'string', required: true, positional: true, help: 'Post ID (e.g. 1abc123) or fullname (t3_xxx)' },
        { name: 'undo', type: 'boolean', default: false, help: 'Unsave instead of save' },
    ],
    columns: ['status', 'message'],
    func: async (page, kwargs) => {
        if (!page)
            throw new CommandExecutionError('Browser session required');
        await page.goto('https://www.reddit.com');
        const result = await page.evaluate(`(async () => {
      try {
        let postId = ${JSON.stringify(kwargs['post-id'])};
        const urlMatch = postId.match(/comments\\/([a-z0-9]+)/);
        if (urlMatch) postId = urlMatch[1];
        const fullname = postId.startsWith('t3_') || postId.startsWith('t1_')
          ? postId : 't3_' + postId;

        const undo = ${kwargs.undo ? 'true' : 'false'};
        const endpoint = undo ? '/api/unsave' : '/api/save';

        // Get modhash
        const meRes = await fetch('/api/me.json', { credentials: 'include' });
        const me = await meRes.json();
        const modhash = me?.data?.modhash || '';

        const res = await fetch(endpoint, {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Start a browser session (login) before running the reddit save command
  2. Verify the browser launched successfully and check earlier logs for launch errors
  3. Ensure the command is invoked through the path that injects the `page` argument into `func`
  4. Catch CommandExecutionError and prompt the user to open a browser session first

Example fix

// before
await runCli(['reddit', 'save', postId]);
// after
const page = await openBrowserSession();
if (!page) throw new Error('Start a browser session first');
await runCli(['reddit', 'save', postId], { page });
Defensive patterns

Strategy: validation

Validate before calling

// Check before invoking the command:
if (!page) throw new Error('Open a browser session (login) before running reddit save');

Type guard

function hasBrowserSession(page) {
  return page != null && typeof page.goto === 'function' && typeof page.evaluate === 'function';
}

Try / catch

try {
  await runCli(['reddit', 'save', postId]);
} catch (e) {
  if (e.message === 'Browser session required') {
    await startSessionAndLogin();
    await runCli(['reddit', 'save', postId]);
  } else throw e;
}

Prevention

When it happens

Trigger: Running the reddit save command without first starting/attaching a browser session, e.g. invoking the CLI in a mode where the page argument is undefined, or the browser failed to launch/attach before the command ran.

Common situations: Running commands via a script that skips the browser bootstrap; browser launch failure upstream that left page null; invoking the command headlessly without the session plugin enabled.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/3436b95db62a91e4. Report an issue: GitHub.