DIYgod/RSSHub · error · Error
Unknown type: ${type}
Error message
Unknown type: ${type} What it means
Route /whu/swrh/:type only accepts integer types 0–2 (学院新闻, 学术科研, 通知公告 per the description table). Number.parseInt is applied to the param; anything outside {0,1,2} (including NaN) falls to the default branch.
Source
Thrown at lib/routes/whu/swrh.ts:58
async function handler(ctx) {
const type = Number.parseInt(ctx.req.param('type'));
let link;
switch (type) {
case 0:
link = `${baseUrl}/index/xyxw.htm`; // 学院新闻
break;
case 1:
link = `${baseUrl}/index/xsky.htm`; // 学术科研
break;
case 2:
link = `${baseUrl}/xxgk/tzgg.htm`; // 通知公告
break;
default:
throw new Error(`Unknown type: ${type}`);
}
const response = await got(link);
const $ = load(response.data);
const list =
type === 0
? $('div.my_box_nei')
.toArray()
.map((item): DataItem & { link: string } => {
const $item = $(item);
return {
title: $item.find('a b.am-text-truncate').text().trim(),
pubDate: $item.find('a i').text(),
link: new URL($item.find('a').attr('href')!, baseUrl).href,
};
})
: $('div.list_txt.am-fr ul.am-list li')View on GitHub (pinned to bed535e087)
Solutions
- Use 0, 1, or 2 only (note: swrh has 3 categories, cs has 4).
- Verify the path: /whu/swrh/<0|1|2>.
- If you need a 4th category, use /whu/cs instead.
Example fix
// before /whu/swrh/3 // after /whu/swrh/2 // 通知公告
Defensive patterns
Strategy: validation
Validate before calling
const VALID = new Set([0, 1, 2]);
const type = Number.parseInt(ctx.req.param('type'), 10);
if (!Number.isInteger(type) || !VALID.has(type)) {
throw new InvalidParameterError(`/whu/swrh/:type must be 0, 1, or 2`);
} Type guard
function isWhuSwrhType(v: unknown): v is 0 | 1 | 2 {
const n = Number(v);
return Number.isInteger(n) && n >= 0 && n <= 2;
} Try / catch
try {
// handler
} catch (e) {
if (e instanceof Error && /^Unknown type:/.test(e.message)) {
throw new InvalidParameterError('Use type 0, 1, or 2 for /whu/swrh');
}
throw e;
} Prevention
- Note swrh (0–2) and cs (0–3) differ — do not copy example values blindly.
- Validate at handler entry and throw InvalidParameterError for clean 4xx mapping.
- Unit-test the switch against the full valid set plus one out-of-range value.
When it happens
Trigger: Caller requests /whu/swrh/3, /whu/swrh/xyz, or any non {0,1,2} value; non-numeric input yields NaN which matches no case.
Common situations: Confusing this route with /whu/cs which supports type 3 (科研进展); typo; passing a string category name.
Related errors
AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12).
Data as JSON: /api/errors/b692b5c75dd4df3a.
Report an issue: GitHub.