From 0512f799cda3439f22092b9982bddee00936932a Mon Sep 17 00:00:00 2001 From: Luna McNulty Date: Fri, 1 Dec 2023 21:02:16 -0500 Subject: [PATCH 1/2] Add debugging info to parseNews response --- site/gatsby-site/src/api/parseNews.js | 30 +++++++++++-------- .../submissions/SubmissionWizard.js | 28 ++++++++++++----- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/site/gatsby-site/src/api/parseNews.js b/site/gatsby-site/src/api/parseNews.js index 4c8b941f86..8e957d13a7 100644 --- a/site/gatsby-site/src/api/parseNews.js +++ b/site/gatsby-site/src/api/parseNews.js @@ -5,22 +5,26 @@ import axios from 'axios'; const stripImages = /!\[[^\]]*\]\((?.*?)(?="|\))(?".*")?\)/g; export default async function handler(req, res) { - const { url } = req.query; + try { + const { url } = req.query; - const article = await getArticle(url, { cookies: false }); + const article = await getArticle(url, { cookies: false }); - const response = { - title: article.title, - authors: article.author, - date_published: article.date_published - ? format(parseISO(article.date_published), 'yyyy-MM-dd') - : null, - date_downloaded: format(new Date(), 'yyyy-MM-dd'), - image_url: article.lead_image_url, - text: article.content?.replace(stripImages, '').trim(), - }; + const response = { + title: article.title, + authors: article.author, + date_published: article.date_published + ? format(parseISO(article.date_published), 'yyyy-MM-dd') + : null, + date_downloaded: format(new Date(), 'yyyy-MM-dd'), + image_url: article.lead_image_url, + text: article.content?.replace(stripImages, '').trim(), + }; - res.status(200).json(response); + res.status(200).json(response); + } catch (error) { + res.status(500).send([error.message, error.stack].filter((e) => e).join('\n\n')); + } } // Runs first with { cookies: false }, diff --git a/site/gatsby-site/src/components/submissions/SubmissionWizard.js b/site/gatsby-site/src/components/submissions/SubmissionWizard.js index 53da6b382f..a75beaf19c 100644 --- a/site/gatsby-site/src/components/submissions/SubmissionWizard.js +++ b/site/gatsby-site/src/components/submissions/SubmissionWizard.js @@ -76,7 +76,20 @@ const SubmissionWizard = ({ const response = await fetch(url); if (!response.ok) { - throw new Error('Parser error'); + let errorText = 'Parser error'; + + let responseErrorText = null; + + try { + responseErrorText = await response.text(); + } catch (e) { + responseErrorText = 'Could not read parser error:\n\n' + e.message; + } + + if (responseErrorText) { + errorText += '\n\n' + responseErrorText; + } + throw new Error(errorText); } const news = await response.json(); @@ -122,13 +135,12 @@ const SubmissionWizard = ({ setData(newValues); } catch (e) { - const message = - e.message == 'Parser error' - ? t( - `Error fetching news. Scraping was blocked by {{newsUrl}}. Please enter the text manually.`, - { newsUrl } - ) - : t(`Error reaching news info endpoint, please try again in a few seconds.`); + const message = e.message.startsWith('Parser error') + ? t( + `Error fetching news. Scraping was blocked by {{newsUrl}}. Please enter the text manually.`, + { newsUrl } + ) + : t(`Error reaching news info endpoint, please try again in a few seconds.`); addToast({ message: <>{message}, From bc70aacf45b26d1d88cfb661d6fc415521a6815b Mon Sep 17 00:00:00 2001 From: Luna McNulty Date: Sat, 2 Dec 2023 05:31:39 +0000 Subject: [PATCH 2/2] Add space --- site/gatsby-site/src/components/submissions/SubmissionWizard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/gatsby-site/src/components/submissions/SubmissionWizard.js b/site/gatsby-site/src/components/submissions/SubmissionWizard.js index a75beaf19c..404d4e7884 100644 --- a/site/gatsby-site/src/components/submissions/SubmissionWizard.js +++ b/site/gatsby-site/src/components/submissions/SubmissionWizard.js @@ -83,7 +83,7 @@ const SubmissionWizard = ({ try { responseErrorText = await response.text(); } catch (e) { - responseErrorText = 'Could not read parser error:\n\n' + e.message; + responseErrorText = 'Could not read parser error:' + '\n\n' + e.message; } if (responseErrorText) {