From 18373626b243a2d6db63905a2fb9d126dcd59e61 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Mon, 15 Apr 2024 19:27:40 -0700 Subject: [PATCH] fix: catch parse error --- backend/functions/src/services/puppeteer.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/functions/src/services/puppeteer.ts b/backend/functions/src/services/puppeteer.ts index 176876d..c888a92 100644 --- a/backend/functions/src/services/puppeteer.ts +++ b/backend/functions/src/services/puppeteer.ts @@ -118,12 +118,21 @@ export class PuppeteerControl extends AsyncService { preparations.push(page.evaluateOnNewDocument(READABILITY_JS)); preparations.push(page.evaluateOnNewDocument(` function giveSnapshot() { + let parsedContent; + try { + // Attempt to parse the cloned document + parsedContent = new Readability(document.cloneNode(true)).parse(); + } catch (error) { + // If an error occurs, log it and set parsedContent to undefined + parsedContent = undefined; + } + return { title: document.title, href: document.location.href, html: document.documentElement.outerHTML, text: document.body.innerText, - parsed: new Readability(document.cloneNode(true)).parse(), + parsed: parsedContent }; } `));