jackwener/OpenCLI · error · CommandExecutionError

Browser session required

Error message

Browser session required

What it means

CommandExecutionError thrown by the `reddit comment` command when it is invoked without an active browser session: the func receives page === null. Commenting requires a logged-in, cookie-backed browser context on reddit.com, so the command refuses to run without one.

Source

Thrown at clis/reddit/comment.js:18

import { CommandExecutionError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';
cli({
    site: 'reddit',
    name: 'comment',
    access: 'write',
    description: 'Post a comment on 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: 'text', type: 'string', required: true, positional: true, help: 'Comment text' },
    ],
    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 text = ${JSON.stringify(kwargs.text)};

        // 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('/api/comment', {
          method: 'POST',

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run the reddit login flow first so a browser session exists, then retry the comment command.
  2. Ensure the command is invoked with the browser/browser-session option enabled (the command requires browser: true).
  3. Verify the persisted browser profile path exists and is writable so the session can be restored.
  4. If scripting, establish the session programmatically before calling comment instead of calling it cold.

Example fix

// before: cold call, page is null
opencli reddit comment 1abc123 "nice post"
// after: authenticate first
opencli reddit login && opencli reddit comment 1abc123 "nice post"
Defensive patterns

Strategy: validation

Validate before calling

if (!page || page.isClosed?.()) {
  throw new Error('Run `reddit login` to establish a browser session before commenting.');
}

Try / catch

try {
  await opencli.reddit.comment(postId, text);
} catch (e) {
  if (/Browser session required/.test(e.message)) {
    await opencli.reddit.login();
    return opencli.reddit.comment(postId, text);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the comment command in a non-browser context or before any login/session is established — the registry invokes func with page=null (e.g. no `--browser`/no persisted session), and the guard `if (!page) throw` fires.

Common situations: Running the command in a fresh environment with no browser profile; scripting the CLI without first running the reddit login; a configuration change that disables the browser strategy for the command.

Related errors


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