jackwener/OpenCLI · error · ArgumentError

pubmed ${label} must be a numeric PMID

Error message

pubmed ${label} must be a numeric PMID

What it means

requirePmid validates that the input is a bare numeric PubMed ID. Non-numeric values throw ArgumentError 'pubmed <label> must be a numeric PMID' with the example '37780221', catching DOIs, PMCIDs, titles, and multi-ID pastes before an NCBI call.

Source

Thrown at clis/pubmed/utils.js:21

export const EUTILS_BASE = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils';
export const SEARCH_COLUMNS = ['rank', 'pmid', 'title', 'authors', 'journal', 'year', 'article_type', 'doi', 'url'];
export const LINK_COLUMNS = ['rank', 'pmid', 'title', 'authors', 'journal', 'year', 'article_type', 'doi', 'url'];
export const RELATED_COLUMNS = ['rank', 'pmid', 'title', 'authors', 'journal', 'year', 'article_type', 'score', 'doi', 'url'];

let lastRequestAt = 0;

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

export function requirePmid(value, label = 'pmid') {
    const pmid = requireText(value, label);
    if (!/^\d+$/.test(pmid)) {
        throw new ArgumentError(`pubmed ${label} must be a numeric PMID`, 'Example: 37780221');
    }
    return pmid;
}

export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
    const raw = value ?? defaultValue;
    const text = String(raw).trim();
    if (!/^\d+$/.test(text)) {
        throw new ArgumentError(`pubmed ${label} must be a positive integer`);
    }
    const n = Number(text);
    if (!Number.isSafeInteger(n) || n < 1) {
        throw new ArgumentError(`pubmed ${label} must be a positive integer`);
    }
    if (n > maxValue) {
        throw new ArgumentError(`pubmed ${label} must be <= ${maxValue}`);
    }
    return n;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a pure digit PMID, e.g. 37780221
  2. Run `pubmed search` first to resolve a title/DOI/query to a PMID, then use that ID
  3. Strip surrounding text/whitespace; do not add an 'PMC' prefix

Example fix

// before
await pubmedPmid('10.1038/s41586-023-06600-9');
// after
const rows = await pubmedSearch('10.1038/s41586-023-06600-9');
await pubmedPmid(rows[0].pmid); // e.g. '37780221'
Defensive patterns

Strategy: validation

Validate before calling

if (!/^\d+$/.test(String(pmid ?? '').trim())) {
  throw new Error(`'${pmid}' is not a numeric PMID (example: 37780221)`);
}

Type guard

function isPmid(v) {
  return typeof v === 'string' || typeof v === 'number'
    ? /^\d+$/.test(String(v).trim())
    : false;
}

Try / catch

try {
  await pubmedPmid(input);
} catch (err) {
  if (err instanceof ArgumentError && /numeric PMID/.test(err.message)) {
    const resolved = await pubmedSearch(input); // resolve DOI/title to PMID
    await pubmedPmid(resolved[0].pmid);
  } else { throw err; }
}

Prevention

When it happens

Trigger: Calling `pubmed pmid <value>` with '10.1038/...' (DOI), 'PMC1234567', an article title, or '123 456' (multiple IDs).

Common situations: Confusing PMCID with PMID, pasting a DOI where a PMID is expected, passing a search query instead of a resolved ID.

Related errors


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