jackwener/OpenCLI · error · CommandExecutionError

Browser session required

Error message

Browser session required

What it means

The reddit `saved` command (listing saved posts) requires an authenticated browser `page` to fetch https://www.reddit.com and read the user's saved items. If no page is passed to `func` it throws this CommandExecutionError immediately. Like the save command, it cannot operate without an active browser session.

Source

Thrown at clis/reddit/saved.js:17

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';
cli({
    site: 'reddit',
    name: 'saved',
    access: 'read',
    description: 'Browse your saved Reddit posts',
    domain: 'reddit.com',
    strategy: Strategy.COOKIE,
    browser: true,
    args: [
        { name: 'limit', type: 'int', default: 15 },
    ],
    columns: ['title', 'subreddit', 'score', 'comments', 'url'],
    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 {
        // Get current username
        const meRes = await fetch('/api/me.json?raw_json=1', { credentials: 'include' });
        const me = await meRes.json();
        const username = me?.name || me?.data?.name;
        if (!username) return { error: 'Not logged in — cannot determine username' };

        const limit = ${kwargs.limit};
        const res = await fetch('/user/' + username + '/saved.json?limit=' + limit + '&raw_json=1', {
          credentials: 'include'
        });
        const d = await res.json();
        return (d?.data?.children || []).map(c => ({
          title: c.data.title || c.data.body?.slice(0, 100) || '',
          subreddit: c.data.subreddit_name_prefixed || 'r/' + (c.data.subreddit || '?'),
          score: c.data.score || 0,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Open and authenticate a browser session before running `reddit saved`
  2. Check that the browser process is running and the page handle is valid
  3. Ensure your runner passes the live page into the command's func
  4. Handle CommandExecutionError by falling back to an explicit 'please log in' message

Example fix

// before
const items = await runCli(['reddit', 'saved', '--limit', '20']);
// after
if (!(await hasActiveSession())) await loginReddit();
const items = await runCli(['reddit', 'saved', '--limit', '20']);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a live, logged-in page before listing saved posts:
if (!page) throw new Error('Browser session required — run the login command first');

Type guard

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

Try / catch

try {
  const saved = await runCli(['reddit', 'saved', '--limit', '20']);
} catch (e) {
  if (e.message === 'Browser session required') {
    await startSessionAndLogin();
    return runCli(['reddit', 'saved', '--limit', '20']);
  }
  throw e;
}

Prevention

When it happens

Trigger: Running the reddit saved command with no browser session attached — page is falsy when func is invoked, typically because no session was started or the session layer failed to provide a page.

Common situations: CI/headless environments where the browser never launched; forgetting to call the login/session-open command first; a crashed browser leaving the session handle null.

Related errors


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