{"record":{"id":"8b58207f5c4ebe71","repo":"DIYgod/RSSHub","slug":"failed-to-fetch-data-from-api","errorCode":null,"errorMessage":"Failed to fetch data from API","messagePattern":"Failed to fetch data from API","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/routes/bnu/fe.ts","lineNumber":41,"sourceCode":"\nasync function handler(ctx) {\n    const { category } = ctx.req.param();\n    const apiUrl = 'https://fe.bnu.edu.cn/pc/cmscommon/nlist';\n    let response;\n    try {\n        // 发送 POST 请求\n        response = await got.post(apiUrl, {\n            headers: {\n                Accept: 'application/json, text/javascript, */*; q=0.01',\n                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',\n                Origin: 'https://fe.bnu.edu.cn',\n                Referer: 'https://fe.bnu.edu.cn/pc/cms1info/list/1/18',\n                'X-Requested-With': 'XMLHttpRequest',\n            },\n            body: `columnid=${category}&page=1`, // POST 数据\n        });\n    } catch {\n        throw new Error('Failed to fetch data from API');\n    }\n    const jsonData = JSON.parse(response.body);\n    // 检查返回的 code\n    if (jsonData.code !== 0 || !jsonData.data) {\n        throw new Error('Invalid API response');\n    }\n\n    const list = jsonData.data.map((item) => ({\n        title: item.title,\n        link: `https://fe.bnu.edu.cn/html/1/news/${item.htmlpath}/n${item.newsid}.html`,\n        pubDate: parseDate(item.happendate, 'YYYY-MM-DD'),\n    }));\n\n    const out = await Promise.all(\n        list.map((item) =>\n            cache.tryGet(item.link, async () => {\n                const response = await got(item.link);\n                const $ = load(response.data);","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/routes/bnu/fe.ts#L23-L59","documentation":"Generic Error from a catch block wrapping got.post to the BNU (Beijing Normal University) faculty CMS API. Any thrown network/HTTP error is swallowed and re-thrown as this generic message, hiding the original cause. It signals the POST to fe.bnu.edu.cn's column listing endpoint could not complete.","triggerScenarios":"got.post to the apiUrl threw — connection reset, DNS failure, non-2xx status that got treats as an error, or a socket timeout. The catch (no binding) discards the original error and throws the generic string.","commonSituations":"The BNU site is down or rate-limiting; the server IP is blocked by the campus firewall; TLS/cert issues; the hardcoded Referer/Origin headers being rejected after a site change.","solutions":["Retry — campus CMS endpoints are intermittently flaky.","Verify https://fe.bnu.edu.cn is reachable from the RSSHub host (curl the apiUrl with the same headers).","If persistent, inspect the original error by temporarily logging it in the catch (the bare `catch {}` currently swallows it).","Update the Referer/Origin/X-Requested-With headers if the CMS changed its CSRF checks."],"exampleFix":"// before\n} catch {\n    throw new Error('Failed to fetch data from API');\n}\n// after (preserve the cause for diagnosis)\n} catch (e) {\n    throw new Error('Failed to fetch data from API: ' + (e instanceof Error ? e.message : String(e)));\n}","handlingStrategy":"retry","validationCode":"try {\n    response = await got.post(apiUrl, { headers, body: `columnid=${category}&page=1` });\n} catch (e) {\n    throw new Error('Failed to fetch data from API: ' + (e instanceof Error ? e.message : String(e)));\n}","typeGuard":null,"tryCatchPattern":"// Wrap with a bounded retry for transient campus-network failures:\nlet response, lastErr;\nfor (let i = 0; i < 3; i++) {\n    try { response = await got.post(apiUrl, { headers, body }); break; }\n    catch (e) { lastErr = e; await delay(500 * (i + 1)); }\n}\nif (!response) throw new Error('Failed to fetch data from API: ' + lastErr);","preventionTips":["Do not swallow the original error in a bare catch — include its message.","Campus CMS endpoints are flaky; add a short retry with backoff.","Verify reachability from the RSSHub host (firewall/geo) before assuming a code bug."],"tags":["bnu","network","catch-swallow","http-post","diagnostics"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}