feat: Return specific errors on failed parse attempts

pull/264/head^2
Toufic Mouallem 5 years ago committed by GitHub
parent a250f403f5
commit 136d6df798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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);

@ -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 () => {

@ -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 () => {

@ -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,
};
}
}

@ -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;

@ -1,3 +1,2 @@
export { default as range } from './range';
export { default as validateUrl } from './validate-url';
export { default as Errors } from './errors';

Loading…
Cancel
Save