From 136d6df7980e66853307d7636e09102ddf10bc5f Mon Sep 17 00:00:00 2001 From: Toufic Mouallem Date: Wed, 20 Mar 2019 11:23:54 +0200 Subject: [PATCH] feat: Return specific errors on failed parse attempts --- src/mercury.js | 8 ++++++-- src/mercury.test.js | 15 +++++++-------- src/resource/index.test.js | 3 +-- src/resource/utils/fetch-resource.js | 6 ++++-- src/utils/errors.js | 9 --------- src/utils/index.js | 1 - 6 files changed, 18 insertions(+), 24 deletions(-) delete mode 100644 src/utils/errors.js diff --git a/src/mercury.js b/src/mercury.js index 4b37d851..4931e035 100644 --- a/src/mercury.js +++ b/src/mercury.js @@ -3,7 +3,7 @@ import cheerio from 'cheerio'; import TurndownService from 'turndown'; import Resource from 'resource'; -import { validateUrl, Errors } from 'utils'; +import { validateUrl } from 'utils'; import getExtractor from 'extractors/get-extractor'; import RootExtractor from 'extractors/root-extractor'; import collectAllPages from 'extractors/collect-all-pages'; @@ -27,7 +27,11 @@ const Mercury = { const parsedUrl = URL.parse(url); if (!validateUrl(parsedUrl)) { - return Errors.badUrl; + return { + error: true, + message: + 'The url parameter passed does not look like a valid URL. Please check your URL and try again.', + }; } const $ = await Resource.create(url, html, parsedUrl); diff --git a/src/mercury.test.js b/src/mercury.test.js index 175418c0..cb693de6 100644 --- a/src/mercury.test.js +++ b/src/mercury.test.js @@ -1,5 +1,4 @@ import assert from 'assert'; -import { Errors } from 'utils'; import { record } from 'test-helpers'; import Mercury from './mercury'; @@ -15,13 +14,13 @@ describe('Mercury', () => { it('returns an error if a malformed url is passed', async () => { const error = await Mercury.parse('foo.com'); - assert.equal(error, Errors.badUrl); + assert(/does not look like a valid URL/i.test(error.message)); }); it('returns an error if a bad url is passed', async () => { const error = await Mercury.parse('foo.com'); - assert.equal(error, Errors.badUrl); + assert(/does not look like a valid URL/i.test(error.message)); }); it('does the whole thing', async () => { @@ -38,15 +37,15 @@ describe('Mercury', () => { 'https://www.thekitchn.com/instant-pot-chicken-pesto-pasta-eating-instantly-267141' ); - assert.equal(error, Errors.badUrl); + assert(/instructed to reject non-2xx/i.test(error.message)); }); - it('does blogger', async () => { - const result = await Mercury.parse( - 'https://googleblog.blogspot.com/2016/08/onhub-turns-one-today.html' + it('returns an error on invalid content types', async () => { + const error = await Mercury.parse( + 'https://upload.wikimedia.org/wikipedia/commons/5/52/Spacer.gif' ); - assert.equal(typeof result, 'object'); + assert(/content-type for this resource/i.test(error.message)); }); it('does blogger', async () => { diff --git a/src/resource/index.test.js b/src/resource/index.test.js index 4dfaa0f8..a0905f92 100644 --- a/src/resource/index.test.js +++ b/src/resource/index.test.js @@ -1,6 +1,5 @@ import assert from 'assert'; import cheerio from 'cheerio'; -import { Errors } from 'utils'; import { getEncoding } from 'utils/text'; import { record } from 'test-helpers'; @@ -23,7 +22,7 @@ describe('Resource', () => { const url = 'http://nytimes.com/500'; const error = await Resource.create(url); - assert.equal(error, Errors.badUrl); + assert(/instructed to reject non-2xx/i.test(error.message)); }); it('fetches with different encoding on body', async () => { diff --git a/src/resource/utils/fetch-resource.js b/src/resource/utils/fetch-resource.js index f4ad4f3a..96972f06 100644 --- a/src/resource/utils/fetch-resource.js +++ b/src/resource/utils/fetch-resource.js @@ -1,6 +1,5 @@ import URL from 'url'; import request from 'postman-request'; -import { Errors } from 'utils'; import { REQUEST_HEADERS, @@ -119,6 +118,9 @@ export default async function fetchResource(url, parsedUrl) { response, }; } catch (e) { - return Errors.badUrl; + return { + error: true, + message: e.message, + }; } } diff --git a/src/utils/errors.js b/src/utils/errors.js deleted file mode 100644 index 4e76e0d4..00000000 --- a/src/utils/errors.js +++ /dev/null @@ -1,9 +0,0 @@ -const Errors = { - badUrl: { - error: true, - messages: - 'The url parameter passed does not look like a valid URL. Please check your data and try again.', - }, -}; - -export default Errors; diff --git a/src/utils/index.js b/src/utils/index.js index 75047c3e..c5c075e1 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,3 +1,2 @@ export { default as range } from './range'; export { default as validateUrl } from './validate-url'; -export { default as Errors } from './errors';