jackwener/OpenCLI · error · ArgumentError

weread-official: ${label} cannot be empty

Error message

weread-official: ${label} cannot be empty

What it means

requireText trims its input and throws ArgumentError if nothing remains. Every text parameter (query, keyword, note text) passes through it, guaranteeing the gateway never receives empty strings (which the gateway would silently drop from the flattened body).

Source

Thrown at clis/weread-official/utils.js:247

    return `weread://reading?bId=${bid}`;
}

/**
 * Split a WeRead `range` field ("900-2004") into `{rangeStart, rangeEnd}`.
 * Returns empty strings when the input is missing/malformed.
 */
export function parseRange(range) {
    const text = String(range ?? '').trim();
    const match = text.match(/^(\d+)-(\d+)$/);
    if (!match) return { rangeStart: '', rangeEnd: '' };
    return { rangeStart: match[1], rangeEnd: match[2] };
}

// ── Argument validation ─────────────────────────────────────────────────────

export function requireText(value, label) {
    const text = String(value ?? '').trim();
    if (!text) throw new ArgumentError(`weread-official: ${label} cannot be empty`);
    return text;
}

export function requireBookId(value, label = 'bookId') {
    const text = requireText(value, label);
    if (!/^[A-Za-z0-9_-]+$/.test(text)) {
        throw new ArgumentError(`weread-official: ${label} contains invalid characters`, 'Pass a bookId from `weread-official search`.');
    }
    return text;
}

export function requirePositiveInt(value, label, { defaultValue, max } = {}) {
    if (value === undefined || value === null || value === '') {
        if (defaultValue === undefined) {
            throw new ArgumentError(`weread-official: ${label} is required`);
        }
        return defaultValue;
    }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Supply a non-empty value for the flagged parameter.
  2. Check shell variables with `echo "-${QUERY}-"` to spot unset/empty values.
  3. Guard with requireText or a length check at the entry point of your script.
  4. Remove the empty flag rather than passing an empty value.

Example fix

// before
const query = process.env.QUERY ?? '';
await keyword(query);
// after
const query = process.env.QUERY;
if (!query || !query.trim()) throw new Error('QUERY env var is required');
await keyword(query);
Defensive patterns

Strategy: validation

Validate before calling

function requireNonEmpty(v, label) {
  const t = String(v ?? '').trim();
  if (!t) throw new Error(`${label} must be a non-empty string`);
  return t;
}
const query = requireNonEmpty(process.env.QUERY, 'QUERY');

Type guard

const isNonEmptyText = (v) => typeof v === 'string' && v.trim().length > 0;

Try / catch

try {
  return await keyword(rawQuery);
} catch (e) {
  if (e instanceof ArgumentError && /cannot be empty/.test(e.message)) {
    console.error(`Empty ${label} — check the flag/env var feeding it`);
    process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Passing '' , ' ', null, or undefined to requireText-backed helpers like keyword or text; a CLI flag provided but empty (e.g. --query ""); a variable that was never assigned.

Common situations: Empty shell variable expansion ($QUERY unset); copy-paste of whitespace-only text; interactive prompt cancelled leaving empty input; script reading an empty file as the query.

Related errors


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