Skip to content

Commit

Permalink
feat: Return specific errors on failed parse attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
toufic-m authored Mar 20, 2019
1 parent a250f40 commit 136d6df
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 24 deletions.
8 changes: 6 additions & 2 deletions src/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
15 changes: 7 additions & 8 deletions src/mercury.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'assert';
import { Errors } from 'utils';

import { record } from 'test-helpers';
import Mercury from './mercury';
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
3 changes: 1 addition & 2 deletions src/resource/index.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 () => {
Expand Down
6 changes: 4 additions & 2 deletions src/resource/utils/fetch-resource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import URL from 'url';
import request from 'postman-request';
import { Errors } from 'utils';

import {
REQUEST_HEADERS,
Expand Down Expand Up @@ -119,6 +118,9 @@ export default async function fetchResource(url, parsedUrl) {
response,
};
} catch (e) {
return Errors.badUrl;
return {
error: true,
message: e.message,
};
}
}
9 changes: 0 additions & 9 deletions src/utils/errors.js

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as range } from './range';
export { default as validateUrl } from './validate-url';
export { default as Errors } from './errors';

0 comments on commit 136d6df

Please sign in to comment.