DIYgod/RSSHub · error · Error

无法正确获取快递公司列表:请稍后重试

Error message

无法正确获取快递公司列表:请稍后重试

What it means

Thrown by kuaidi100's getCompanyList when parsing the company.do JS response fails. The handler fetches a JS file from kuaidi100, slices the first 12 chars, replaces `};`→`}` and single quotes→double quotes, then JSON.parses it and reads `.company`. If any of those string transforms no longer yield valid JSON (JSON.parse throws), the catch block re-throws this Error ('无法正确获取快递公司列表' = 'could not correctly obtain the courier company list').

Source

Thrown at lib/routes/kuaidi100/utils.ts:124

        const cookie = await getCookie();
        const wwwid = cookie.wwwid;
        const companyResponse = await got({
            method: 'post',
            url: 'https://www.kuaidi100.com/company.do?method=js&t=201701051440',
            headers: {
                Referer: 'https://www.kuaidi100.com/',
                Cookie: wwwid,
            },
        });
        let list = companyResponse.body;

        // Parsing the js file
        try {
            list = list.slice(12).replaceAll('};', '}').replaceAll("'", '"');
            list = JSON.parse(list);
            list = list.company;
        } catch {
            throw new Error('无法正确获取快递公司列表:请稍后重试');
        }

        return list;
    });
}

function shouldUpdateCookie(forcedUpdate = false) {
    if (forcedUpdate) {
        cache.set(query_count, 0 as unknown as string);
    } else {
        const count = cache.get(query_count) as unknown as number | null;
        if (count) {
            if (count > max_query_count) {
                cache.set(query_count, 0 as unknown as string);
                clearCookie();
            } else {
                cache.set(query_count, (count + 1) as unknown as string);
            }

View on GitHub (pinned to bed535e087)

Solutions

  1. Fetch https://www.kuaidi100.com/company.do?method=js&t=201701051440 manually and inspect the raw body to see how the format changed
  2. Update the normalization (slice offset, replaceAll patterns) in lib/routes/kuaidi100/utils.ts:120 to match the new shape
  3. Ensure getCookie() returns a valid wwwid — an expired/empty cookie often yields a non-JS response
  4. Bust the daily `kuaidi100-company-name-<date>` cache key after fixing the parser

Example fix

// before
try {
    list = list.slice(12).replaceAll('};', '}').replaceAll("'", '"');
    list = JSON.parse(list);
    list = list.company;
} catch {
    throw new Error('无法正确获取快递公司列表:请稍后重试');
}
// after — keep the raw body in the error for diagnosis
try {
    list = list.slice(12).replaceAll('};', '}').replaceAll("'", '"');
    list = JSON.parse(list).company;
} catch (e) {
    throw new Error(`无法正确获取快递公司列表:请稍后重试 (parse failed on body length ${list.length}: ${(e as Error).message})`);
}
Defensive patterns

Strategy: retry

Validate before calling

async function companyListParseable(): Promise<boolean> {
  try {
    const cookie = await getCookie();
    const resp = await got({ method: 'post', url: 'https://www.kuaidi100.com/company.do?method=js&t=201701051440', headers: { Referer: 'https://www.kuaidi100.com/', Cookie: cookie.wwwid ?? '' } });
    const body = resp.body;
    JSON.parse(body.slice(12).replaceAll('};', '}').replaceAll("'", '"')).company;
    return true;
  } catch { return false; }
}

Type guard

interface KuaidiCompany { number: string; name: string; checkReg?: string }
function isCompanyList(v: unknown): v is KuaidiCompany[] {
  return Array.isArray(v) && v.every((c) => c && typeof c.number === 'string');
}

Try / catch

try { return await getCompanyList(); }
catch (e) {
  if (e instanceof Error && /快递公司列表/.test(e.message)) {
    // parsing failed — cookie may be stale; clear and retry once
    cache.set(`kuaidi100-company-name-${new Date().toISOString().split('T', 1)[0]}`, null);
    return await getCompanyList();
  }
  throw e;
}

Prevention

When it happens

Trigger: The company.do endpoint returns content whose prefix/suffix/quote structure changed (e.g. 快递100 rebuilt the JS bundle), or returns an HTML error/anti-bot page instead of the JS payload, so the slice(12)+replaceAll normalization no longer produces parseable JSON.

Common situations: Site rebuild altering the JS template; anti-bot middleware replacing the response with HTML; cookie/wwwid expired causing a redirect to a login page returned as the body; upstream content-type change.

Related errors


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