{"record":{"id":"564552c6092c396a","repo":"DIYgod/RSSHub","slug":"error-564552","errorCode":null,"errorMessage":"无法正确获取快递公司列表：请稍后重试","messagePattern":"无法正确获取快递公司列表：请稍后重试","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/routes/kuaidi100/utils.ts","lineNumber":124,"sourceCode":"        const cookie = await getCookie();\n        const wwwid = cookie.wwwid;\n        const companyResponse = await got({\n            method: 'post',\n            url: 'https://www.kuaidi100.com/company.do?method=js&t=201701051440',\n            headers: {\n                Referer: 'https://www.kuaidi100.com/',\n                Cookie: wwwid,\n            },\n        });\n        let list = companyResponse.body;\n\n        // Parsing the js file\n        try {\n            list = list.slice(12).replaceAll('};', '}').replaceAll(\"'\", '\"');\n            list = JSON.parse(list);\n            list = list.company;\n        } catch {\n            throw new Error('无法正确获取快递公司列表：请稍后重试');\n        }\n\n        return list;\n    });\n}\n\nfunction shouldUpdateCookie(forcedUpdate = false) {\n    if (forcedUpdate) {\n        cache.set(query_count, 0 as unknown as string);\n    } else {\n        const count = cache.get(query_count) as unknown as number | null;\n        if (count) {\n            if (count > max_query_count) {\n                cache.set(query_count, 0 as unknown as string);\n                clearCookie();\n            } else {\n                cache.set(query_count, (count + 1) as unknown as string);\n            }","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/routes/kuaidi100/utils.ts#L106-L142","documentation":"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').","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fetch https://www.kuaidi100.com/company.do?method=js&t=201701051440 manually and inspect the raw body to see how the format changed","Update the normalization (slice offset, replaceAll patterns) in lib/routes/kuaidi100/utils.ts:120 to match the new shape","Ensure getCookie() returns a valid wwwid — an expired/empty cookie often yields a non-JS response","Bust the daily `kuaidi100-company-name-<date>` cache key after fixing the parser"],"exampleFix":"// before\ntry {\n    list = list.slice(12).replaceAll('};', '}').replaceAll(\"'\", '\"');\n    list = JSON.parse(list);\n    list = list.company;\n} catch {\n    throw new Error('无法正确获取快递公司列表：请稍后重试');\n}\n// after — keep the raw body in the error for diagnosis\ntry {\n    list = list.slice(12).replaceAll('};', '}').replaceAll(\"'\", '\"');\n    list = JSON.parse(list).company;\n} catch (e) {\n    throw new Error(`无法正确获取快递公司列表：请稍后重试 (parse failed on body length ${list.length}: ${(e as Error).message})`);\n}","handlingStrategy":"retry","validationCode":"async function companyListParseable(): Promise<boolean> {\n  try {\n    const cookie = await getCookie();\n    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 ?? '' } });\n    const body = resp.body;\n    JSON.parse(body.slice(12).replaceAll('};', '}').replaceAll(\"'\", '\"')).company;\n    return true;\n  } catch { return false; }\n}","typeGuard":"interface KuaidiCompany { number: string; name: string; checkReg?: string }\nfunction isCompanyList(v: unknown): v is KuaidiCompany[] {\n  return Array.isArray(v) && v.every((c) => c && typeof c.number === 'string');\n}","tryCatchPattern":"try { return await getCompanyList(); }\ncatch (e) {\n  if (e instanceof Error && /快递公司列表/.test(e.message)) {\n    // parsing failed — cookie may be stale; clear and retry once\n    cache.set(`kuaidi100-company-name-${new Date().toISOString().split('T', 1)[0]}`, null);\n    return await getCompanyList();\n  }\n  throw e;\n}","preventionTips":["Bust the daily company-list cache when the parser changes","Validate the response is JS (not HTML) before parsing","Keep the slice/replaceAll offsets documented against the upstream JS template","Add a fallback to a static company list for read-only routes"],"tags":["parsing","scraping","site-restructure","transient"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}