From c859291ca3812d99e4b84201733e0447f4f33f26 Mon Sep 17 00:00:00 2001 From: Cloud Date: Wed, 4 Sep 2019 11:41:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20if=20fulltext=20parser=20failed,=20retur?= =?UTF-8?q?n=20default=20description=20and=E2=80=A6=20(#2992)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/middleware/parameter.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/middleware/parameter.js b/lib/middleware/parameter.js index 3a2117a936..27e87d62b9 100644 --- a/lib/middleware/parameter.js +++ b/lib/middleware/parameter.js @@ -19,16 +19,21 @@ module.exports = async (ctx, next) => { const tasks = ctx.state.data.item.map(async (item) => { const { link, author, description } = item; const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => { - const res = await got(link); - const $ = cheerio.load(res.data); - const result = await mercury_parser.parse(link, { - html: $.html(), - }); - return result; + // if parser failed, return default description and not report error + try { + const res = await got(link); + const $ = cheerio.load(res.data); + const result = await mercury_parser.parse(link, { + html: $.html(), + }); + return result; + } catch (e) { + // no-empty + } }); - item.author = author || parsed_result.author; - item.description = parsed_result.content || description; + item.author = author || (parsed_result ? parsed_result.author : ''); + item.description = parsed_result ? parsed_result.content : description; }); await Promise.all(tasks); }