jackwener/OpenCLI · error · ArgumentError

wikidata ${label} cannot be empty

Error message

wikidata ${label} cannot be empty

What it means

requireString is the wikidata adapter's shared guard for mandatory string arguments (e.g. the search query). It stringifies the value, trims it, and throws an ArgumentError if nothing remains. This fails fast with a clear message instead of sending an empty query to the Wikidata API.

Source

Thrown at clis/wikidata/utils.js:19

// Shared helpers for the Wikidata adapters.
//
// Wikidata exposes two complementary public endpoints:
//   • `wbsearchentities` on `www.wikidata.org/w/api.php` for keyword → Q-IDs
//   • `Special:EntityData/<qid>.json` for the canonical entity dump
// No API key. Anonymous traffic is rate-limited but generous; we set a polite UA.
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';

export const WIKIDATA_BASE = 'https://www.wikidata.org';
const UA = 'opencli-wikidata-adapter/1.0 (+https://github.com/jackwener/opencli; mailto:opencli@example.com)';

// Q-ID = an item; P-ID = a property; L-ID = a lexeme. We accept all three so the
// adapter can be reused for properties / lexemes without a separate command, but
// search only returns Q-IDs by default.
const ENTITY_ID_PATTERN = /^[QPL]\d+$/;

export function requireString(value, label) {
    const s = String(value ?? '').trim();
    if (!s) throw new ArgumentError(`wikidata ${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(`wikidata ${label} must be a positive integer`);
    }
    if (n > maxValue) {
        throw new ArgumentError(`wikidata ${label} must be <= ${maxValue}`);
    }
    return n;
}

export function requireEntityId(value) {
    const raw = String(value ?? '').trim().toUpperCase();
    if (!raw) throw new ArgumentError('wikidata entity id is required (e.g. "Q937")');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Provide a non-empty value for the labeled argument (e.g. the search query)
  2. Check the environment variable or config key feeding the argument is actually set
  3. Trim/validate inputs in your script before invoking the command

Example fix

// before
const query = process.env.QUERY; // may be undefined
await runCli(['wikidata', 'search', query]);
// after
const query = process.env.QUERY?.trim();
if (!query) throw new Error('QUERY env var must be a non-empty search term');
await runCli(['wikidata', 'search', query]);
Defensive patterns

Strategy: validation

Validate before calling

const query = String(rawQuery ?? '').trim();
if (!query) throw new Error('wikidata search requires a non-empty query');
await runCli(['wikidata', 'search', query]);

Type guard

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

Try / catch

try {
    await runCli(['wikidata', 'search', query]);
} catch (e) {
    if (/cannot be empty/.test(e.message)) {
        console.error('Provide a search term');
        process.exitCode = 2;
    } else throw e;
}

Prevention

When it happens

Trigger: Calling requireString (directly or via the `query` argument path of wikidata search) with undefined, null, '', or a whitespace-only string for the labeled argument.

Common situations: An empty shell variable passed to the CLI (`wikidata search "$Q"` with Q unset); a config file with a blank query field; a script building args programmatically that drops the query.

Related errors


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