jackwener/OpenCLI · error · ArgumentError

coingecko currency must look like a currency slug (got "${ar

Error message

coingecko currency must look like a currency slug (got "${args.currency}")

What it means

The `coingecko global` command validates the `currency` argument against the slug pattern /^[a-z0-9-]{2,20}$/ after trimming and lowercasing; anything that doesn't match (too short/long, or containing spaces, symbols, or uppercase) throws this ArgumentError before any network call. The value must look like a plausible quote-currency slug (usd, cny, eur, jpy, ...).

Source

Thrown at clis/coingecko/global.js:21

import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, CommandExecutionError } from '@jackwener/opencli/errors';

cli({
    site: 'coingecko',
    name: 'global',
    access: 'read',
    description: 'Aggregate crypto market stats: total market cap, volume, dominance',
    domain: 'api.coingecko.com',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'currency', type: 'string', default: 'usd', help: 'Quote currency for total market cap / volume (usd, cny, eur, jpy, ...)' },
    ],
    columns: ['currency', 'totalMarketCap', 'totalVolume24h', 'marketCapChange24hPct', 'btcDominancePct', 'ethDominancePct', 'activeCryptocurrencies', 'markets', 'ongoingIcos', 'updatedAt'],
    func: async (args) => {
        const currency = String(args.currency ?? 'usd').trim().toLowerCase();
        if (!/^[a-z0-9-]{2,20}$/.test(currency)) {
            throw new ArgumentError(`coingecko currency must look like a currency slug (got "${args.currency}")`);
        }
        let resp;
        try {
            resp = await fetch('https://api.coingecko.com/api/v3/global', { headers: { 'User-Agent': 'Mozilla/5.0' } });
        }
        catch (err) {
            throw new CommandExecutionError(`coingecko global request failed: ${err?.message ?? err}`);
        }
        if (resp.status === 429) {
            throw new CommandExecutionError(
                'coingecko returned HTTP 429 (rate limited)',
                'Free tier allows ~30 calls/min. Wait and retry.',
            );
        }
        if (!resp.ok) {
            throw new CommandExecutionError(`coingecko global returned HTTP ${resp.status}`);
        }
        let body;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use a lowercase currency slug of 2-20 chars: `--currency usd` (default), eur, cny, jpy, gbp.
  2. Sanitize input first: `currency = String(raw).trim().toLowerCase().replace(/[^a-z0-9-]/g, '')` before calling.
  3. If the input is a currency name ('US Dollar'), map it to its slug ('usd') before invoking.
  4. Note: passing this pattern check does not guarantee CoinGecko supports the currency — an unsupported slug later raises 'coingecko has no market totals for currency'.

Example fix

// before
await globalCmd.func({ currency: 'US Dollar' });
// after
const slug = String(raw).trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
await globalCmd.func({ currency: slug || 'usd' });
Defensive patterns

Strategy: validation

Validate before calling

const currency = String(raw ?? 'usd').trim().toLowerCase();
if (!/^[a-z0-9-]{2,20}$/.test(currency)) throw new Error(`unsupported currency slug: ${raw}`);

Type guard

function isCurrencySlug(v) { return typeof v === 'string' && /^[a-z0-9-]{2,20}$/.test(v); }

Try / catch

try {
  await globalCmd.func({ currency });
} catch (e) {
  if (/must look like a currency slug/.test(e.message)) {
    return globalCmd.func({ currency: 'usd' }); // safe fallback
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing currency values like '', 'u', 'US DOLLAR', 'us dollar', 'usd!', '$usd', or a 25+ character string — e.g. `coingecko global --currency 'us dollar'` or `func({ currency: 'USD$' })`.

Common situations: Users typing human-readable currency names instead of slugs, values with trailing whitespace or punctuation copied from elsewhere, uppercase currency codes pasted without normalization (the library lowercases, but embedded spaces/symbols still fail), or empty strings from unset environment variables.

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/de4fb05e50c49e92. Report an issue: GitHub.