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

  1. Pass a whole number >= 0, e.g. --offset 0 for the first page.
  2. Fix the pagination arithmetic producing a negative offset (clamp with Math.max(0, n)).
  3. 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

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


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