jackwener/OpenCLI · warning · EmptyResultError
rest-countries region
Error message
rest-countries region
What it means
The rest-countries region command throws EmptyResultError when the /v3.1/region/{region} endpoint returns an empty list for a recognized region. This means the request was valid but the API returned no countries for that region string. The library raises it explicitly rather than returning an empty array.
Source
Thrown at clis/rest-countries/region.js:59
'languages',
'currencies',
'latitude',
'longitude',
'timezones',
'independent',
'unMember',
'landlocked',
'flag',
'url',
],
func: async (args) => {
const region = requireRegion(args.region);
const limit = requireBoundedInt(args.limit, 250, 250);
const url = `${REST_COUNTRIES_BASE}/region/${encodeURIComponent(region)}?fields=${COUNTRY_FIELDS}`;
const body = await restCountriesFetch(url, 'rest-countries region');
const list = Array.isArray(body) ? body : [];
if (!list.length) {
throw new EmptyResultError('rest-countries region', `No countries returned for region "${region}".`);
}
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
- Retry the request; empty responses for a valid region are usually transient.
- Verify the region value against the allowed set (africa, americas, asia, europe, oceania).
- Check the raw endpoint in a browser/curl to confirm the API returns data.
- Catch EmptyResultError and surface a friendly 'no data right now' message.
Example fix
// before
await regionCommand({ region: 'Antarctica' });
// after
await regionCommand({ region: 'europe' }); Defensive patterns
Strategy: retry
Validate before calling
const REGIONS = new Set(['africa','americas','asia','europe','oceania']);
if (!REGIONS.has(String(args.region ?? '').trim().toLowerCase())) {
throw new Error('invalid or unknown region');
} Type guard
function isKnownRegion(v) {
return ['africa','americas','asia','europe','oceania'].includes(String(v ?? '').trim().toLowerCase());
} Try / catch
try {
const countries = await regionCommand({ region });
} catch (err) {
if (err instanceof EmptyResultError) {
// empty payload for a valid region: retry once, then surface 'no data'
} else {
throw err;
}
} Prevention
- Retry once on empty responses for valid regions before surfacing failure.
- Only use the API's five canonical region values.
- Verify upstream health (curl the region endpoint) if empties persist.
- Cache region results to reduce dependence on transient upstream state.
When it happens
Trigger: Calling the rest-countries region command where `requireRegion` accepted the value but `restCountriesFetch` returned an empty body, hitting `!list.length` at clis/rest-countries/region.js:59 (e.g. transient upstream data issues or an unusual region slug).
Common situations: Upstream API temporarily returning empty payloads; region value that passes local validation but the API maps to nothing; network proxies stripping response bodies.
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/388b31aa4657ba7e.
Report an issue: GitHub.