Skip to content

Commit

Permalink
Merge pull request #199 from eshaham/error_handling
Browse files Browse the repository at this point in the history
Error handling
  • Loading branch information
eshaham authored Jun 10, 2019
2 parents f457d19 + 5d049ac commit 1b1cddb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const LOGIN_RESULT = {
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
};

export const NAVIGATION_ERRORS = {
export const ERRORS = {
TIMEOUT: 'TIMEOUT',
GENERIC: 'GENERIC',
};
Expand Down
5 changes: 0 additions & 5 deletions src/helpers/navigation.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/waiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
19 changes: 11 additions & 8 deletions src/scrapers/base-scraper.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 {
Expand All @@ -41,7 +40,7 @@ class BaseScraper {
} catch (e) {
loginResult = e.timeout ?
createTimeoutError(e.message) :
createGenericNavigationError(e.message);
createGenericError(e.message);
}

let scrapeResult;
Expand All @@ -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;
Expand Down

0 comments on commit 1b1cddb

Please sign in to comment.