jackwener/OpenCLI · error · ArgumentError

mdn limit must be a positive integer

Error message

mdn limit must be a positive integer

What it means

requireBoundedInt validates the limit parameter: it defaults the value, coerces to number, and requires a positive integer. If the value is not an integer or is <= 0, it throws this ArgumentError. The MDN search API requires a valid size parameter.

Source

Thrown at clis/mdn/search.js:23

// canonical MDN URL.
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';

const MDN_BASE = 'https://developer.mozilla.org';
const UA = 'opencli-mdn-adapter (+https://github.com/jackwener/opencli)';
const ALLOWED_LOCALES = new Set(['en-US', 'de', 'es', 'fr', 'ja', 'ko', 'pt-BR', 'ru', 'zh-CN', 'zh-TW']);

function requireString(value, label) {
    const s = String(value ?? '').trim();
    if (!s) throw new ArgumentError(`mdn ${label} cannot be empty`);
    return s;
}

function requireBoundedInt(value, defaultValue, maxValue) {
    const raw = value ?? defaultValue;
    const n = typeof raw === 'number' ? raw : Number(raw);
    if (!Number.isInteger(n) || n <= 0) {
        throw new ArgumentError('mdn limit must be a positive integer');
    }
    if (n > maxValue) {
        throw new ArgumentError(`mdn limit must be <= ${maxValue}`);
    }
    return n;
}

function requireLocale(value) {
    const s = String(value ?? 'en-US').trim();
    if (!ALLOWED_LOCALES.has(s)) {
        throw new ArgumentError(
            `mdn locale "${value}" is not supported`,
            `Allowed locales: ${[...ALLOWED_LOCALES].join(' / ')}`,
        );
    }
    return s;
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a positive integer, e.g. --limit 10.
  2. Check the value: Number.isInteger(Number(v)) && Number(v) > 0 before calling.
  3. Fix config/env sources that supply non-numeric or zero limit values.
  4. Omit the option to use the library default instead of an invalid value.

Example fix

// before
await mdnSearch({ query: 'fetch api', limit: '5x' });
// after
const limit = Number.parseInt(rawLimit, 10);
await mdnSearch({ query: 'fetch api', limit: Number.isInteger(limit) && limit > 0 ? limit : undefined });
Defensive patterns

Strategy: validation

Validate before calling

const n = Number.parseInt(rawLimit, 10);
if (!Number.isInteger(n) || n <= 0) throw new RangeError('limit must be a positive integer');

Type guard

function isPositiveInt(v) { return typeof v === 'number' && Number.isInteger(v) && v > 0; }

Try / catch

try { await mdnSearch({ query, limit }); } catch (e) { if (String(e.message).includes('limit must be a positive integer')) { limit = 10; return retry(); } throw e; }

Prevention

When it happens

Trigger: Passing a limit of 0, a negative number, a non-numeric string (e.g. 'ten', ''), a float like 2.5, or NaN to the mdn search limit option.

Common situations: CLI flag typo (--limit abc), parsing limits from config files that hold strings like '5x', dividing values producing floats, forgetting that empty string coerces to 0.

Related errors


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