jackwener/OpenCLI · warning · EmptyResultError

rest-countries country

Error message

rest-countries country

What it means

The rest-countries country command throws EmptyResultError when the /v3.1/name/{name} endpoint returns an empty array, meaning no country matched the given name. The library converts that into an explicit, labeled error so callers see 'no countries matched' instead of silently getting []. It normalizes a valid HTTP 200 empty response into a searchable error.

Source

Thrown at clis/rest-countries/country.js:59

        'languages',
        'currencies',
        'latitude',
        'longitude',
        'timezones',
        'independent',
        'unMember',
        'landlocked',
        'flag',
        'url',
    ],
    func: async (args) => {
        const name = requireString(args.name, 'name');
        const limit = requireBoundedInt(args.limit, 25, 250);
        const url = `${REST_COUNTRIES_BASE}/name/${encodeURIComponent(name)}?fields=${COUNTRY_FIELDS}`;
        const body = await restCountriesFetch(url, 'rest-countries country');
        const list = Array.isArray(body) ? body : [];
        if (!list.length) {
            throw new EmptyResultError('rest-countries country', `No countries matched "${name}".`);
        }
        // Sort by population descending so the most "expected" hit is first.
        const sorted = [...list].sort((a, b) => (b?.population ?? 0) - (a?.population ?? 0));
        return sorted.slice(0, limit).map((c, i) => ({ rank: i + 1, ...projectCountry(c) }));
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Check the spelling of the country name argument.
  2. Use the official English country name (e.g. 'United States' not 'USA' variants the API may not match).
  3. Catch EmptyResultError and offer the user a suggestion/fuzzy match flow.
  4. If needed, query a different endpoint (e.g. /v3.1/translation/ or /v3.1/currency/) that matches the input you have.

Example fix

// before
await countryCommand({ name: 'Kanada' });
// after
await countryCommand({ name: 'Canada' });
Defensive patterns

Strategy: validation

Validate before calling

if (typeof args.name !== 'string' || !args.name.trim()) {
  throw new Error('country name must be a non-empty string');
}

Type guard

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

Try / catch

try {
  const countries = await countryCommand({ name });
} catch (err) {
  if (err instanceof EmptyResultError) return suggestSimilarNames(name);
  throw err;
}

Prevention

When it happens

Trigger: Calling the rest-countries country command with `args.name` whose encoded value yields `list.length === 0` at clis/rest-countries/country.js:59, e.g. misspelled or non-country names like 'Narnia' or 'Kanada'.

Common situations: Misspelled country names ('Geramny'); using partial/informal names the API does not match; stale cached name lists; passing localized names in another language.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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