jackwener/OpenCLI · error · ArgumentError
Invalid subreddit name.
Error message
Invalid subreddit name.
What it means
`parseSubredditName` throws this ArgumentError when the name, after stripping an optional `r/` or `/r/` prefix, fails SUBREDDIT_NAME_RE (/^[A-Za-z][A-Za-z0-9_]{2,20}$/). Valid subreddit names are 3–21 characters, must start with a letter, and contain only letters, digits, and underscores. The library validates locally so bad names never reach Reddit's API.
Source
Thrown at clis/reddit/subreddit-info.js:19
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';
// Reddit subreddit names: 3–21 chars, letters/digits/underscore, must start
// with a letter. Accept an optional `r/` prefix and normalise it off.
const SUBREDDIT_NAME_RE = /^[A-Za-z][A-Za-z0-9_]{2,20}$/;
export function parseSubredditName(raw) {
let name = String(raw || '').trim();
if (!name) {
throw new ArgumentError(
'Subreddit name is required.',
'Pass a subreddit name like `python` (or `r/python`).',
);
}
if (name.startsWith('/r/')) name = name.slice(3);
else if (name.startsWith('r/')) name = name.slice(2);
if (!SUBREDDIT_NAME_RE.test(name)) {
throw new ArgumentError(
'Invalid subreddit name.',
'Subreddit names are 3–21 characters, start with a letter, and contain only letters, digits, and underscores.',
);
}
return name;
}
cli({
site: 'reddit',
name: 'subreddit-info',
access: 'read',
description: 'Show metadata for a Reddit subreddit (subscribers, description, created date, NSFW)',
domain: 'reddit.com',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'name', type: 'string', required: true, positional: true, help: 'Subreddit name (no `r/` prefix needed)' },
],View on GitHub (pinned to 49907e53dc)
Solutions
- Pass only the bare subreddit name: `sub python` or `sub r/python`, not a URL
- Strip 'https://www.reddit.com/r/' prefixes from pasted URLs, keeping just the name segment
- Verify the name is 3–21 chars, starts with a letter, and uses only [A-Za-z0-9_]
- Validate with the same regex in your own code before calling, to give a friendlier message
Example fix
// before await runCli(['reddit', 'sub', 'https://www.reddit.com/r/AskProgramming/']); // invalid // after const name = 'https://www.reddit.com/r/AskProgramming/'.match(/reddit\.com\/r\/([A-Za-z0-9_]+)/)?.[1]; await runCli(['reddit', 'sub', name]); // 'AskProgramming'
Defensive patterns
Strategy: validation
Validate before calling
// Mirror the library's own regex before calling:
const SUBREDDIT_NAME_RE = /^[A-Za-z][A-Za-z0-9_]{2,20}$/;
function toSubredditName(raw) {
let name = String(raw ?? '').trim().replace(/^\/r\//, '').replace(/^r\/, '');
name = name.replace(/^https?:\/\/[^/]*reddit\.com\/r\//, '').split('/')[0];
if (!SUBREDDIT_NAME_RE.test(name)) throw new Error(`Invalid subreddit name: ${name}`);
return name;
} Type guard
function isValidSubredditName(v) {
return typeof v === 'string' && /^[A-Za-z][A-Za-z0-9_]{2,20}$/.test(v.replace(/^(\/r\/|r\/)/, ''));
} Try / catch
try {
await runCli(['reddit', 'sub', raw]);
} catch (e) {
if (e instanceof ArgumentError && /Invalid subreddit name/.test(e.message)) {
console.error('Names are 3-21 chars, start with a letter, [A-Za-z0-9_] only. Pass `python` or `r/python`.');
} else throw e;
} Prevention
- Reduce pasted subreddit URLs to the bare name before calling
- Reject names with hyphens, dots, spaces, or non-Latin characters early
- Enforce the 3–21 char / letter-first rule in your own input layer
When it happens
Trigger: Passing names like 'ab' (too short), '4chan' (starts with digit — though real subs cannot start with digits anyway), names with hyphens/dots/spaces, URLs like 'reddit.com/r/python' that were not reduced to just the name, or names longer than 21 characters.
Common situations: Pasting a full subreddit URL instead of the name; passing 'r/' with nothing after it; including query strings or trailing slashes; typos or non-Latin characters in the name.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- coingecko currency must look like a currency slug (got "${ar
- designer must be a Dribbble username or profile slug
- lichess username "${value}" is not a valid handle. Allowed:
- packagist package "${value}" is not a valid Composer name
- Subreddit name is required.
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/5b346181cd016cb4.
Report an issue: GitHub.