jackwener/OpenCLI · error · ArgumentError

openalex ${label} cannot be empty

Error message

openalex ${label} cannot be empty

What it means

requireString validates that a labeled user-supplied argument (e.g. the search `query`) is a non-empty, non-whitespace string, and throws ArgumentError otherwise. The library throws it early so an empty request never reaches the OpenAlex API. It coerces null/undefined to '' first, so any falsy or whitespace-only input triggers the error.

Source

Thrown at clis/openalex/utils.js:22

// unauthenticated; passing an email via `mailto=` opts into the polite pool
// (faster). Work IDs are `W` followed by digits (`W2741809807`) and
// round-trip via `https://api.openalex.org/works/<id>` or
// `https://openalex.org/W…`.
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';

export const OPENALEX_BASE = 'https://api.openalex.org';
const UA = 'opencli-openalex-adapter (+https://github.com/jackwener/opencli)';

// OpenAlex stable IDs: a single-letter prefix (`W` works, `A` authors, `S`
// sources, `I` institutions…) + at least 4 digits. We accept just `W` here.
const WORK_ID = /^W\d{4,}$/;
// DOIs are loose — accept anything starting with "10." after the optional
// `doi.org/` prefix; OpenAlex itself does the normalization.
const DOI_BARE = /^10\.\S+$/;

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

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

/**
 * Resolve a user-supplied work identifier to OpenAlex's canonical path
 * segment. Accepts `W…` IDs, `doi:10.…`, raw DOIs, or full

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Provide a non-empty query string to the search command
  2. Check that the environment variable or config value feeding the query is set
  3. Trim the input yourself and fail with a clearer message before invoking the library

Example fix

// before
const q = process.env.QUERY; // '' when unset
await search(q);
// after
const q = (process.env.QUERY ?? '').trim();
if (!q) { console.error('QUERY env var must be set'); process.exit(1); }
await search(q);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

function isNonEmptyString(v) {
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  await search(query);
} catch (e) {
  if (e.name === 'ArgumentError' && e.message.includes('cannot be empty')) {
    console.error('Supply a non-empty query'); process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Passing `''`, `' '`, `null`, or `undefined` as the value for a labeled argument — concretely, an empty `query` to openalex search, since requireString is called by query.

Common situations: Shell variable holding the query is unset/empty (`$Q` expands to nothing); CLI flag provided without a value; upstream pipeline produced no query text; reading query from config where the key is missing.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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