From d190d9d7001c0afeccc5c5787fa3d1b6c503ba8e Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 26 Mar 2024 00:33:36 +0800 Subject: [PATCH] test: ofetch got --- lib/routes/bloomberg/utils.ts | 4 ++-- lib/routes/gov/chinatax/latest.ts | 2 +- lib/routes/line/utils.ts | 2 +- lib/routes/phoronix/index.ts | 2 +- lib/routes/wechat/feeddd.ts | 2 +- lib/routes/wfu/news.ts | 2 +- lib/utils/got.test.ts | 8 ++------ package.json | 2 +- pnpm-lock.yaml | 19 ++++++++----------- 9 files changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/routes/bloomberg/utils.ts b/lib/routes/bloomberg/utils.ts index b48da1258..0bcbbefc7 100644 --- a/lib/routes/bloomberg/utils.ts +++ b/lib/routes/bloomberg/utils.ts @@ -99,7 +99,7 @@ const parseArticle = (item) => res = await got(apiUrl, { headers }); } catch (error) { // fallback - if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError')) { + if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError' || error.name === 'FetchError')) { try { res = await got(item.link, { headers }); } catch { @@ -214,7 +214,7 @@ const parseReactRendererPage = async (res, api, item) => { return await parseStoryJson(res.data, item); } catch (error) { // fallback - if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError')) { + if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError' || error.name === 'FetchError')) { return { title: item.title, link: item.link, diff --git a/lib/routes/gov/chinatax/latest.ts b/lib/routes/gov/chinatax/latest.ts index 6b54a4c73..9999b6e49 100644 --- a/lib/routes/gov/chinatax/latest.ts +++ b/lib/routes/gov/chinatax/latest.ts @@ -56,7 +56,7 @@ async function handler() { item.description = content('#fontzoom').html(); return item; } catch (error) { - if (error.name === 'HTTPError') { + if (error.name === 'HTTPError' || error.name === 'FetchError') { item.description = error.message; return item; } diff --git a/lib/routes/line/utils.ts b/lib/routes/line/utils.ts index e55c00d46..8d9712e48 100644 --- a/lib/routes/line/utils.ts +++ b/lib/routes/line/utils.ts @@ -29,7 +29,7 @@ const parseItems = (list, tryGet) => }); data = response.data; } catch (error) { - if (error instanceof got.HTTPError && error.response.statusCode === 404) { + if ((error.name === 'HTTPError' || error.name === 'FetchError') && error.response.statusCode === 404) { logger.error(`Error parsing article ${item.link}: ${error.message}`); return item; } diff --git a/lib/routes/phoronix/index.ts b/lib/routes/phoronix/index.ts index d4063906a..e4413fd4b 100644 --- a/lib/routes/phoronix/index.ts +++ b/lib/routes/phoronix/index.ts @@ -72,7 +72,7 @@ const webFetch = (url) => try { return webFetchCb(await got(url)); } catch (error) { - if (error.name === 'HTTPError' && error.response.statusCode === 404) { + if ((error.name === 'HTTPError' || error.name === 'FetchError') && error.response.statusCode === 404) { return '404'; } throw error; diff --git a/lib/routes/wechat/feeddd.ts b/lib/routes/wechat/feeddd.ts index d71c01a4f..97a4a52c3 100644 --- a/lib/routes/wechat/feeddd.ts +++ b/lib/routes/wechat/feeddd.ts @@ -13,7 +13,7 @@ export default async (ctx) => { try { response = await got(apiUrl); } catch (error) { - if (error.name === 'HTTPError' && error.response.statusCode === 404) { + if ((error.name === 'HTTPError' || error.name === 'FetchError') && error.response.statusCode === 404) { throw new Error('该公众号不存在,有关如何获取公众号 id,详见 https://docs.rsshub.app/routes/new-media#wei-xin-gong-zhong-hao-feeddd-lai-yuan'); } throw error; diff --git a/lib/routes/wfu/news.ts b/lib/routes/wfu/news.ts index eefca4f5b..b0bf76ff6 100644 --- a/lib/routes/wfu/news.ts +++ b/lib/routes/wfu/news.ts @@ -32,7 +32,7 @@ async function loadContent(link) { response = await got.get(link); } catch (error) { // 如果网络问题 直接出错 - if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError')) { + if (error.name && (error.name === 'HTTPError' || error.name === 'RequestError' || error.name === 'FetchError')) { description = 'Page 404 Please Check!'; } return { description }; diff --git a/lib/utils/got.test.ts b/lib/utils/got.test.ts index 59d69c234..3b3453a18 100644 --- a/lib/utils/got.test.ts +++ b/lib/utils/got.test.ts @@ -34,7 +34,7 @@ describe('got', () => { try { await got.get('http://rsshub.test/testRerty'); } catch (error: any) { - expect(error.name).toBe('HTTPError'); + expect(error.name).toBe('FetchError'); } // retries @@ -52,11 +52,7 @@ describe('got', () => { test: 1, }, }); - expect(response1.statusCode).toBe(200); - // @ts-expect-error custom property - expect(response1.status).toBe(200); expect(response1.body).toBe('{"code": 0}'); - // @ts-expect-error custom property expect(response1.data.code).toBe(0); }); @@ -77,7 +73,7 @@ describe('got', () => { await got.get('http://rsshub.test/timeout'); throw new Error('Timeout Invalid'); } catch (error: any) { - expect(error.name).toBe('RequestError'); + expect(error.name).toBe('FetchError'); } expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining('http://rsshub.test/timeout')); diff --git a/package.json b/package.json index e4e3b7496..8b39596f5 100644 --- a/package.json +++ b/package.json @@ -164,7 +164,7 @@ "js-beautify": "1.15.1", "lint-staged": "15.2.2", "mockdate": "3.0.5", - "nock": "13.5.4", + "nock": "14.0.0-beta.5", "prettier": "3.2.5", "remark-parse": "11.0.0", "supertest": "6.3.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 004507bce..0fe2cbb25 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -344,8 +344,8 @@ devDependencies: specifier: 3.0.5 version: 3.0.5 nock: - specifier: 13.5.4 - version: 13.5.4 + specifier: 14.0.0-beta.5 + version: 14.0.0-beta.5 prettier: specifier: 3.2.5 version: 3.2.5 @@ -2072,7 +2072,7 @@ packages: engines: {node: '>= 14'} dependencies: eslint: 8.57.0 - jschardet: 3.1.0 + jschardet: 3.1.2 lodash: 4.17.21 utf8: 3.0.0 transitivePeerDependencies: @@ -5963,8 +5963,8 @@ packages: resolution: {integrity: sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==} dev: false - /jschardet@3.1.0: - resolution: {integrity: sha512-MND0yjRsoQ/3iFXce7lqV/iBmqH9oWGUTlty36obRBZjhFDWCLKjXgfxY75wYfwlW7EFqw52tyziy/q4WsQmrA==} + /jschardet@3.1.2: + resolution: {integrity: sha512-mw3CBZGzW8nUBPYhFU2ztZ/kJ6NClQUQVpyzvFMfznZsoC///ZQ30J2RCUanNsr5yF22LqhgYr/lj807/ZleWA==} engines: {node: '>=0.1.90'} dev: true @@ -6886,15 +6886,12 @@ packages: lower-case: 1.1.4 dev: false - /nock@13.5.4: - resolution: {integrity: sha512-yAyTfdeNJGGBFxWdzSKCBYxs5FxLbCg5X5Q4ets974hcQzG1+qCxvIyOo4j2Ry6MUlhWVMX4OoYDefAIIwupjw==} - engines: {node: '>= 10.13'} + /nock@14.0.0-beta.5: + resolution: {integrity: sha512-u255tf4DYvyErTlPZA9uTfXghiZZy+NflUOFONPVKZ5tP0yaHwKig28zyFOLhu8y5YcCRC+V5vDk4HHileh2iw==} + engines: {node: '>= 18'} dependencies: - debug: 4.3.4 json-stringify-safe: 5.0.1 propagate: 2.0.1 - transitivePeerDependencies: - - supports-color dev: true /node-fetch-native@1.6.4: