jackwener/OpenCLI · error · ArgumentError
${name} must be a non-negative integer (got "${raw}")
Error message
${name} must be a non-negative integer (got "${raw}") What it means
parseNonNegativeInteger throws ArgumentError when the value (after applying defaultValue for undefined/null/'') does not parse as a whole number >= 0. It is used for offset-style options where 0 is valid but negatives are not.
Source
Thrown at clis/slock/resolve.js:62
}
return v;
}
export function parsePositiveInteger(value, name, { defaultValue, max } = {}) {
const raw = value === undefined || value === null || value === '' ? defaultValue : value;
const n = parseStrictInteger(raw);
if (!Number.isInteger(n) || n <= 0 || (max !== undefined && n > max)) {
const suffix = max !== undefined ? ` between 1 and ${max}` : ' as a positive integer';
throw new ArgumentError(`${name} must be${suffix} (got "${raw}")`);
}
return n;
}
export function parseNonNegativeInteger(value, name, { defaultValue } = {}) {
const raw = value === undefined || value === null || value === '' ? defaultValue : value;
const n = parseStrictInteger(raw);
if (!Number.isInteger(n) || n < 0) {
throw new ArgumentError(`${name} must be a non-negative integer (got "${raw}")`);
}
return n;
}
function parseStrictInteger(raw) {
if (typeof raw === 'number')
return raw;
const text = String(raw);
if (!/^\d+$/.test(text))
return NaN;
return Number(text);
}
View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a whole number >= 0, e.g. --offset 0 for the first page.
- Fix the pagination arithmetic producing a negative offset (clamp with Math.max(0, n)).
- Omit the option to use the defaultValue.
Example fix
// before const offset = (page - 1) * size; // page=0 -> -25 // after const offset = Math.max(0, (page - 1) * size);
Defensive patterns
Strategy: validation
Validate before calling
const offset = Math.max(0, Number.isInteger(Number(rawOffset)) ? Number(rawOffset) : 0);
if (!Number.isInteger(offset) || offset < 0) throw new Error('offset must be >= 0'); Type guard
const isNonNegativeInt = (v) => typeof v === 'number' && Number.isInteger(v) && v >= 0;
Try / catch
try {
runCommand(['--offset', String(offset)]);
} catch (e) {
if (e instanceof ArgumentError && /non-negative integer/.test(e.message)) {
console.error(`Offset "${offset}" invalid; using 0.`);
runCommand(['--offset', '0']);
} else throw e;
} Prevention
- Clamp computed pagination offsets with Math.max(0, ...).
- Initialize page counters to 1, not 0, when using (page-1)*size arithmetic.
- Validate shell variables are set (${VAR:?msg}) before interpolating into CLI flags.
When it happens
Trigger: Calling a command with an offset option set to a negative number (e.g. --offset -10), a float, or non-numeric text such as 'first' or 'undefined'.
Common situations: Hand-computed pagination offsets going negative on page 0 (`(page-1)*size` with page=0); scripts interpolating unset shell variables into the flag; copying `--offset=-1` from a buggy loop.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- --page must be a positive integer (got ${raw})
- --offset must be a multiple of 10 for DuckDuckGo HTML pagina
- juejin ${label} must be <= ${maxValue}
- openreview ${label} must be a positive integer
- openreview ${label} must be <= ${maxValue}
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/8c6a9b4b417f04ec.
Report an issue: GitHub.