DIYgod/RSSHub · error · Error

Invalid API response

Error message

Invalid API response

What it means

Generic Error validating the parsed JSON from BNU's CMS API: it requires code === 0 AND a truthy data field. A 200-OK response whose envelope indicates failure (non-zero code) or lacks the data array is rejected here, before list mapping dereferences it.

Source

Thrown at lib/routes/bnu/fe.ts:46

    try {
        // 发送 POST 请求
        response = await got.post(apiUrl, {
            headers: {
                Accept: 'application/json, text/javascript, */*; q=0.01',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                Origin: 'https://fe.bnu.edu.cn',
                Referer: 'https://fe.bnu.edu.cn/pc/cms1info/list/1/18',
                'X-Requested-With': 'XMLHttpRequest',
            },
            body: `columnid=${category}&page=1`, // POST 数据
        });
    } catch {
        throw new Error('Failed to fetch data from API');
    }
    const jsonData = JSON.parse(response.body);
    // 检查返回的 code
    if (jsonData.code !== 0 || !jsonData.data) {
        throw new Error('Invalid API response');
    }

    const list = jsonData.data.map((item) => ({
        title: item.title,
        link: `https://fe.bnu.edu.cn/html/1/news/${item.htmlpath}/n${item.newsid}.html`,
        pubDate: parseDate(item.happendate, 'YYYY-MM-DD'),
    }));

    const out = await Promise.all(
        list.map((item) =>
            cache.tryGet(item.link, async () => {
                const response = await got(item.link);
                const $ = load(response.data);
                item.author = '北京师范大学教育学部';
                item.description = $('.news02_div').html() || '暂无详细内容';
                return item;
            })
        )

View on GitHub (pinned to bed535e087)

Solutions

  1. Log jsonData (code + message keys) to see the CMS's own error description and act on it.
  2. Confirm the category path parameter maps to a valid columnid on the live CMS page.
  3. If the envelope shape changed, update the code/data checks and the list-mapping field names.

Example fix

// before
if (jsonData.code !== 0 || !jsonData.data) {
    throw new Error('Invalid API response');
}
// after (surface the CMS code/message)
if (jsonData.code !== 0 || !jsonData.data) {
    throw new Error(`Invalid API response: code=${jsonData.code}, msg=${jsonData.message ?? 'n/a'}`);
}
Defensive patterns

Strategy: validation

Validate before calling

const jsonData = JSON.parse(response.body);
if (jsonData.code !== 0 || !jsonData.data) {
    throw new Error(`Invalid API response: code=${jsonData.code}, msg=${jsonData.message ?? 'n/a'}`);
}

Type guard

const isBnuSuccess = (j: any): boolean =>
    j && j.code === 0 && Array.isArray(j.data);

Prevention

When it happens

Trigger: got.post succeeded and JSON.parse ran, but jsonData.code is not 0 (e.g. category id rejected, auth required) or jsonData.data is null/undefined/empty. The guard throws before `.map` is called on data.

Common situations: Passing a category id the CMS does not recognize; the CMS returning a permission error; the response shape changing so `data` is nested differently; an HTML error page that happened to be JSON-parseable.

Related errors


AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12). Data as JSON: /api/errors/7e10a2d52f5f1029. Report an issue: GitHub.