DIYgod/RSSHub · error

Invalid type: ${type}. Valid types are: ${validTypes}

Error message

Invalid type: ${type}. Valid types are: ${validTypes}

What it means

Thrown by the ZJU math route when the numeric `:type` path parameter does not correspond to any key in `categoryMap`. The handler truncates the param to an integer, looks it up in the map, and on a miss throws with the requested type and the full list of valid types for discoverability.

Source

Thrown at lib/routes/zju/math/index.ts:128

        supportPodcast: false,
        supportScihub: false,
    },
    name: '数学科学学院',
    description: `| 重要通知 | 本科生 | 研究生 | 科研 | 教学 | 人事 | 公示 |
| -------- | ------ | ------ | ---- | ---- | ---- | ---- |
| 0        | 1      | 2      | 3    | 4    | 5    | 6    |`,
    maintainers: ['Alex222222222222'],
    handler,
    url: 'www.math.zju.edu.cn',
};

async function handler(ctx: Context) {
    const type = Math.trunc(Number(ctx.req.param('type')));
    const categoryInfo = categoryMap.get(type);

    if (!categoryInfo) {
        const validTypes = [...categoryMap.keys().toArray()].join(', ');
        throw new Error(`Invalid type: ${type}. Valid types are: ${validTypes}`);
    }

    const categoryUrl = new URL(categoryInfo.id, base).href;
    const newsItems = await fetchNewsItemsByCategory(categoryInfo.id);
    const items = await Promise.all(newsItems.map((item) => enrichNewsItemWithDetails(item, categoryUrl)));

    return {
        title: categoryInfo.title,
        link: categoryUrl,
        item: items,
    };
}

View on GitHub (pinned to bed535e087)

Solutions

  1. Read the valid types straight from the error message and use one of them (the route documents a 0–6 table).
  2. If you derived the number from somewhere, verify it against the route's category table before requesting.
  3. Guard non-numeric input upstream so it doesn't coerce to `NaN`/0 and silently pick the wrong category.

Example fix

// before — out-of-range / non-numeric type
// GET /zju/math/99   -> Invalid type: 99. Valid types are: 0, 1, 2, ...
// GET /zju/math/news -> Invalid type: NaN ...
// after — valid documented type
// GET /zju/math/0
Defensive patterns

Strategy: validation

Validate before calling

function validateMathType(raw, categoryMap) {
    const type = Math.trunc(Number(raw));
    if (!Number.isFinite(type) || !categoryMap.has(type)) {
        throw new Error(`Invalid type: ${raw}. Valid: ${[...categoryMap.keys()].join(', ')}`);
    }
    return type;
}

Type guard

function isValidMathType(raw: string, categoryMap: Map<number, unknown>): raw is `${number}` {
    const n = Math.trunc(Number(raw));
    return Number.isFinite(n) && categoryMap.has(n);
}

Prevention

When it happens

Trigger: The caller requests a `type` value not defined in the math route's `categoryMap` — an out-of-range integer, a non-numeric string that coerces to `NaN`/0, or a category that simply isn't modeled.

Common situations: User guesses a type number outside the documented 0–6 range; passes a category name instead of its numeric code; the URL was constructed with a stale/wrong index. `Math.trunc(Number('abc'))` yielding `NaN` is a common foot-gun.

Related errors


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