jackwener/OpenCLI · error · CliError

FETCH_ERROR

FETCH_ERROR

Error message

HTTP ${resp.status}

What it means

The google trends command fetches https://trends.google.com/trending/rss?geo=<region> and throws this CliError with code FETCH_ERROR when the response is not ok. The hint mentions both the network connection and the region code because Trends rejects invalid geo codes with 4xx statuses.

Source

Thrown at clis/google/trends.js:26

cli({
    site: 'google',
    name: 'trends',
    access: 'read',
    description: 'Get Google Trends daily trending searches',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'region', default: 'US', help: 'Region code (e.g. US, CN, JP)' },
        { name: 'limit', type: 'int', default: 20, help: 'Number of results' },
    ],
    columns: ['title', 'traffic', 'date'],
    func: async (args) => {
        const limit = Math.max(1, Math.min(Number(args.limit), 100));
        const region = encodeURIComponent(args.region);
        const url = `https://trends.google.com/trending/rss?geo=${region}`;
        const resp = await fetch(url);
        if (!resp.ok) {
            throw new CliError('FETCH_ERROR', `HTTP ${resp.status}`, 'Check your network connection or region code');
        }
        const xml = await resp.text();
        const items = parseRssItems(xml, ['title', 'pubDate', 'ht:approx_traffic']);
        if (!items.length) {
            throw new CliError('NOT_FOUND', 'No trending data found', 'Try a different region code');
        }
        return items.slice(0, limit).map(item => ({
            title: item['title'],
            traffic: item['ht:approx_traffic'], // raw string e.g. "1,000,000+", no numeric conversion
            date: item['pubDate'],
        }));
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Verify the geo parameter is a valid uppercase ISO 3166-1 country code supported by Google Trends (e.g. US, GB, DE)
  2. If 429, back off and slow the polling frequency or change IP
  3. Retry after a delay for 5xx statuses
  4. Fetch the URL in a browser to confirm whether the region works interactively
  5. Check network/proxy if all regions fail

Example fix

// before
const region = encodeURIComponent(args.region); // 'us'
const url = `https://trends.google.com/trending/rss?geo=${region}`;
// after
const region = encodeURIComponent(args.region.toUpperCase()); // 'US'
if (!/^[A-Z]{2}$/.test(region)) throw new CliError('VALIDATION', 'Invalid region code', 'Use an ISO country code');
Defensive patterns

Strategy: validation

Validate before calling

// validate region code before invoking
const geo = String(args.region || '').toUpperCase();
if (!/^[A-Z]{2}$/.test(geo)) throw new Error('region must be an ISO 3166-1 alpha-2 code, e.g. US');

Type guard

function isValidRegion(region) {
  return typeof region === 'string' && /^[A-Za-z]{2}$/.test(region);
}

Try / catch

try {
  return await trendsCommand.func(args);
} catch (e) {
  if (e.code === 'FETCH_ERROR') {
    if (e.message.includes('429')) { await sleep(60000); return trendsCommand.func(args); }
    if (e.message.includes('400') || e.message.includes('404')) throw new Error('Invalid region code');
  }
  throw e;
}

Prevention

When it happens

Trigger: fetch() to the trending RSS endpoint returns resp.ok === false — invalid/unsupported geo region code (400/404), HTTP 429 from polling too often, 403 bot/consent rejection, or a Google-side 5xx.

Common situations: Passing a wrong or lowercase geo code where Trends expects an uppercase ISO country code (e.g. 'us' vs 'US') or an unsupported region; scraping trends in a loop and getting rate-limited; Trends' bot-protection interstitial returning non-200; transient outages.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


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