From 8a66bf3763f47a146dc2867619473f7df3634ed4 Mon Sep 17 00:00:00 2001 From: omgimanerd Date: Mon, 2 Oct 2017 15:01:17 -0400 Subject: [PATCH] Fixed breakage when reddit is queried due to invalid URLs --- server.js | 12 +++++++++--- server/api.js | 9 ++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 84b9ba6..46bf648 100644 --- a/server.js +++ b/server.js @@ -62,7 +62,9 @@ app.post('/analytics', (request, response) => { analytics.get(analyticsFile).then(data => { response.status(201).send(data) }).catch(error => { - throw error + logError(request) + logError(error) + response.status(500).send(INTERNAL_ERROR.red) }) }) @@ -74,7 +76,9 @@ app.get('/sources', (request, response) => { response.status(301).redirect(GITHUB_PAGE) } }).catch(error => { - throw error + logError(request) + logError(error) + response.status(500).send(INTERNAL_ERROR.red) }) }) @@ -102,7 +106,9 @@ app.get('/:source?', (request, response) => { if (error.data && error.data.code === api.BAD_SOURCE) { response.status(400).send(formatter.formatHelp(true)) } else { - throw error + logError(request) + logError(error) + response.status(500).send(INTERNAL_ERROR.red) } }) }) diff --git a/server/api.js b/server/api.js index 5a98699..f7a8e59 100644 --- a/server/api.js +++ b/server/api.js @@ -76,8 +76,9 @@ const shortenUrl = url => { // We want to retry the request instantly if it fails. interval: 0, timeout: 5000 - }).then(data => data.id).catch(error => { - throw new ServerError('Error shortening a URL', error) + }).then(data => data.id).catch(() => { + // Return the original URL on failure. + return url }) } @@ -135,8 +136,10 @@ const fetchArticles = source => { * into a moment object. */ return shortenUrl(article.url).then(url => { - article.url = url + article.title = article.title || 'Untitled Article' article.publishedAt = moment(article.publishedAt) + article.description = article.description || 'No description available' + article.url = url return article }) }).then(articles => {