From b6b8d6814cedd532d3d8909e1df52ae76f31a3ba Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Mon, 31 Jan 2022 10:48:11 +0100 Subject: [PATCH] Make `requestDriver` more resilient to errors (#46) If the async request performed in `requestDriver.makeDriver()` fails, it would call the `callback` function with empty parameters but then continue the execution which can lead to the following error and crash of Fredy: ``` Error while trying to scrape data. Received error: Request failed with status code 504 /fredy/lib/services/requestDriver.js:25 if (typeof result.data === 'object' && url.toLowerCase().indexOf('scrapingant') !== -1) { ^ TypeError: Cannot read properties of undefined (reading 'data') at driver (/fredy/lib/services/requestDriver.js:25:23) at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5) ``` --- lib/services/requestDriver.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/services/requestDriver.js b/lib/services/requestDriver.js index 89ccf44..655f3fd 100644 --- a/lib/services/requestDriver.js +++ b/lib/services/requestDriver.js @@ -7,30 +7,29 @@ function makeDriver(headers = {}) { let cookies = ''; return async function driver(context, callback) { - const url = context.url; - let result; try { - result = await axios({ + const url = context.url; + const result = await axios({ url, headers: { ...headers, Cookie: cookies, }, }); + + if (typeof result.data === 'object' && url.toLowerCase().indexOf('scrapingant') !== -1) { + //assume we have gotten a response from scrapingAnt + if (cookies.length === 0) { + cookies = result.data.cookies; + } + callback(null, result.data.content); + } else { + callback(null, result.data); + } } catch (exception) { console.error(`Error while trying to scrape data. Received error: ${exception.message}`); callback(null, []); } - - if (typeof result.data === 'object' && url.toLowerCase().indexOf('scrapingant') !== -1) { - //assume we have gotten a response from scrapingAnt - if (cookies.length === 0) { - cookies = result.data.cookies; - } - callback(null, result.data.content); - } else { - callback(null, result.data); - } }; }