diff --git a/src/constants.js b/src/constants.js index 35370702..90b4cc1a 100644 --- a/src/constants.js +++ b/src/constants.js @@ -27,7 +27,7 @@ export const LOGIN_RESULT = { UNKNOWN_ERROR: 'UNKNOWN_ERROR', }; -export const NAVIGATION_ERRORS = { +export const ERRORS = { TIMEOUT: 'TIMEOUT', GENERIC: 'GENERIC', }; diff --git a/src/helpers/navigation.js b/src/helpers/navigation.js index 8c12fb6a..00614cfc 100644 --- a/src/helpers/navigation.js +++ b/src/helpers/navigation.js @@ -1,10 +1,5 @@ import waitUntil from './waiting'; -export const NAVIGATION_ERRORS = { - TIMEOUT: 'timeout', - GENERIC: 'generic', -}; - export async function waitForNavigation(page, options) { await page.waitForNavigation(options); } diff --git a/src/helpers/waiting.js b/src/helpers/waiting.js index 73e0d801..5e593160 100644 --- a/src/helpers/waiting.js +++ b/src/helpers/waiting.js @@ -2,7 +2,9 @@ function timeoutPromise(ms, promise, description) { const timeout = new Promise((resolve, reject) => { const id = setTimeout(() => { clearTimeout(id); - reject(new Error({ timeout: true, errorMessage: description })); + const error = new Error(description); + error.timeout = true; + reject(error); }, ms); }); diff --git a/src/scrapers/base-scraper.js b/src/scrapers/base-scraper.js index d84ca45c..94105fc8 100644 --- a/src/scrapers/base-scraper.js +++ b/src/scrapers/base-scraper.js @@ -1,7 +1,6 @@ import { EventEmitter } from 'events'; -import { SCRAPE_PROGRESS_TYPES, LOGIN_RESULT } from '../constants'; -import { NAVIGATION_ERRORS } from '../helpers/navigation'; +import { SCRAPE_PROGRESS_TYPES, LOGIN_RESULT, ERRORS } from '../constants'; const SCRAPE_PROGRESS = 'SCRAPE_PROGRESS'; @@ -14,11 +13,11 @@ function createErrorResult(errorType, errorMessage) { } function createTimeoutError(errorMessage) { - return createErrorResult(NAVIGATION_ERRORS.TIMEOUT, errorMessage); + return createErrorResult(ERRORS.TIMEOUT, errorMessage); } -function createGenericNavigationError(errorMessage) { - return createErrorResult(NAVIGATION_ERRORS.GENERIC, errorMessage); +function createGenericError(errorMessage) { + return createErrorResult(ERRORS.GENERIC, errorMessage); } class BaseScraper { @@ -41,7 +40,7 @@ class BaseScraper { } catch (e) { loginResult = e.timeout ? createTimeoutError(e.message) : - createGenericNavigationError(e.message); + createGenericError(e.message); } let scrapeResult; @@ -52,13 +51,17 @@ class BaseScraper { scrapeResult = e.timeout ? createTimeoutError(e.message) : - createGenericNavigationError(e.message); + createGenericError(e.message); } } else { scrapeResult = loginResult; } - await this.terminate(); + try { + await this.terminate(); + } catch (e) { + scrapeResult = createGenericError(e.message); + } this.emitProgress(SCRAPE_PROGRESS_TYPES.END_SCRAPING); return scrapeResult;