jackwener/OpenCLI · error · ArgumentError

stackoverflow related id must be a numeric question id, got

Error message

stackoverflow related id must be a numeric question id, got ${JSON.stringify(args.id)}

What it means

ArgumentError from `stackoverflow related` when the positional `id` argument fails the /^\d+$/ numeric check. The command requires a bare numeric Stack Overflow question id because it is interpolated into the /questions/{id}/related API path. Any non-numeric or empty value is rejected before any network request is made.

Source

Thrown at clis/stackoverflow/related.js:36

cli({
    site: 'stackoverflow',
    name: 'related',
    access: 'read',
    description: 'List Stack Overflow questions related to a given question id.',
    domain: 'stackoverflow.com',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'id', positional: true, required: true, type: 'string', help: 'Stack Overflow question id (numeric, e.g. 79935770).' },
        { name: 'sort', type: 'string', default: 'rank', help: `Sort key: ${SORT_OPTIONS.join(', ')} (rank = SO relevance default).` },
        { name: 'limit', type: 'int', default: 20, help: 'Max related questions (1-100).' },
    ],
    columns: ['rank', 'id', 'title', 'score', 'answers', 'views', 'isAnswered', 'tags', 'author', 'createdAt', 'lastActivityAt', 'url'],
    func: async (args) => {
        const id = String(args.id ?? '').trim();
        if (!/^\d+$/.test(id)) {
            throw new ArgumentError(`stackoverflow related id must be a numeric question id, got ${JSON.stringify(args.id)}`);
        }
        const sort = String(args.sort ?? 'rank').toLowerCase();
        if (!SORT_OPTIONS.includes(sort)) {
            throw new ArgumentError(`stackoverflow related sort must be one of ${SORT_OPTIONS.join(', ')}`);
        }
        const limit = normalizeLimit(args.limit, 20, 100, 'limit');
        const data = await seFetch(`/questions/${encodeURIComponent(id)}/related`, {
            searchParams: {
                order: 'desc',
                sort,
                pagesize: limit,
            },
        });
        const items = ensureItems(data, `stackoverflow related ${id}`);
        return items.slice(0, limit).map((q, i) => ({
            rank: i + 1,
            id: q.question_id,
            title: decodeHtmlEntities(q.title || ''),

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass only the numeric question id, e.g. `stackoverflow related 79935770`.
  2. If you have a full URL, extract the digits first: url.match(/\/questions\/(\d+)/)?.[1].
  3. Confirm you are using a question id (from /questions/...), not an answer or comment id.
  4. In scripts, validate the id with /^\d+$/ before invoking the command.

Example fix

// before
stackoverflow related "https://stackoverflow.com/questions/79935770/why-x"
// after
stackoverflow related 79935770
Defensive patterns

Strategy: validation

Validate before calling

const id = String(rawId ?? '').trim();
if (!/^\d+$/.test(id)) throw new TypeError(`expected numeric SO question id, got ${JSON.stringify(rawId)}`);

Type guard

function isQuestionId(v) { return typeof v === 'string' && /^\d+$/.test(v.trim()); }

Try / catch

try {
  await related(id);
} catch (e) {
  if (e.name === 'ArgumentError' && e.message.includes('numeric question id')) {
    const m = String(id).match(/\/(?:questions|q)\/(\d+)/);
    if (m) return related(m[1]);
  }
  throw e;
}

Prevention

When it happens

Trigger: `stackoverflow related` with an empty id, a question URL pasted whole (https://stackoverflow.com/questions/123/...), an id containing letters or symbols, or a slug like 'how-do-i-x'.

Common situations: Pasting a full SO URL instead of just the numeric id; forgetting the positional argument entirely; scripts passing 'undefined'/'null' from an upstream variable; using an answer id or user id rather than a question id.

Related errors


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