Skip to content

Commit

Permalink
Merge pull request #2463 from lmcnulty/add-debugging-info-to-parseNew…
Browse files Browse the repository at this point in the history
…s-response

Add debugging info to parseNews response
  • Loading branch information
kepae authored Dec 4, 2023
2 parents 772cab5 + bc70aac commit 192f23b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
30 changes: 17 additions & 13 deletions site/gatsby-site/src/api/parseNews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import axios from 'axios';
const stripImages = /!\[[^\]]*\]\((?<filename>.*?)(?="|\))(?<optionalpart>".*")?\)/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 },
Expand Down
28 changes: 20 additions & 8 deletions site/gatsby-site/src/components/submissions/SubmissionWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}</>,
Expand Down

0 comments on commit 192f23b

Please sign in to comment.