Skip to content

Commit

Permalink
Don't log errors when articles are missing fields
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Feb 4, 2020
1 parent f111922 commit fc38ff7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/backend/data/article-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ArticleError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error(message).stack;
}
}
}

module.exports = ArticleError;
3 changes: 2 additions & 1 deletion src/backend/data/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { logger } = require('../utils/logger');
const sanitizeHTML = require('../utils/sanitize-html');
const textParser = require('../utils/text-parser');
const hash = require('./hash');
const ArticleError = require('./article-error');

function toDate(date) {
if (date instanceof Date) {
Expand Down Expand Up @@ -65,7 +66,7 @@ class Post {
if (missing.length) {
const message = `invalid article: missing ${missing.join(', ')}`;
logger.debug(message);
throw new Error(message);
throw new ArticleError(message);
}

// Allow for missing title, but give it one
Expand Down
37 changes: 30 additions & 7 deletions src/backend/feed/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const fetch = require('node-fetch');
const { logger } = require('../utils/logger');
const Post = require('../data/post');
const Feed = require('../data/feed');
const ArticleError = require('../data/article-error');

// Check for cached ETag and Last-Modified info on the feed.
function hasHeaders(feed) {
Expand Down Expand Up @@ -92,9 +93,32 @@ async function getFeedInfo(feed) {
return info;
}

/**
* Convert an array of Articles from the feed parser into Post objects.
* @param {Array<article>} articles to process into posts
*/
function articlesToPosts(articles) {
const posts = [];

articles.forEach(article => {
if (!article) return;
try {
const post = Post.fromArticle(article);
posts.push(post);
} catch (error) {
// If this is just some missing data, ignore the post, otherwise throw.
if (!(error instanceof ArticleError)) {
throw error;
}
}
});

return posts;
}

module.exports = async function processor(job) {
const feed = Feed.parse(job.data);
let articles;
let articles = [];
let info;

try {
Expand All @@ -104,7 +128,7 @@ module.exports = async function processor(job) {
// Log some common cases we see, with a general message if none of these
switch (info.status) {
case 304:
logger.debug(`${info.status} Feed is up-to-date: ${feed.url}`);
logger.info(`${info.status} Feed is up-to-date: ${feed.url}`);
break;
case 404:
logger.warn(`${info.status} Feed not found: ${feed.url}`);
Expand All @@ -120,16 +144,16 @@ module.exports = async function processor(job) {
logger.warn(`${info.status} Feed server error: ${feed.url}`);
break;
default:
logger.debug(`${info.status} Feed not downloaded: ${feed.url}`);
logger.info(`${info.status} Feed not downloaded: ${feed.url}`);
break;
}

// No posts were processed, return an empty list.
return [];
return articles;
}

// Download the updated feed contents
logger.debug(`${info.status} Feed has new content: ${feed.url}`);
logger.info(`${info.status} Feed has new content: ${feed.url}`);
articles = await parse(
addHeaders(
{
Expand All @@ -142,7 +166,7 @@ module.exports = async function processor(job) {
)
);
// Transform the list of articles to a list of Post objects
articles = articles.map(article => Post.fromArticle(article));
articles = articlesToPosts(articles);

// Version info for this feed changed, so update the database
feed.etag = feed.etag || info.etag;
Expand All @@ -156,7 +180,6 @@ module.exports = async function processor(job) {
info.contentType ? `(${info.contentType})` : ''
}`
);
articles = [];
} else {
logger.error({ error }, `Unable to process feed ${feed.url}`);
throw error;
Expand Down

0 comments on commit fc38ff7

Please sign in to comment.