Skip to content

Commit

Permalink
feat(github): Better support Github API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Sep 10, 2024
1 parent 7869b30 commit c5231eb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
36 changes: 23 additions & 13 deletions client/src/pages/actions/actionsOpenalexFeedback.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@ export default function ActionsOpenalexFeedback({ allOpenalexCorrections }) {
const [validEmail, setValidEmail] = useState(null);
const { toast } = useToast();

const openModal = () => {
const switchModal = () => {
setIsModalOpen((prev) => !prev);
};

const feedback = () => {
sendGitHubIssue({ data: allOpenalexCorrections, email: userEmail });
toast({
description: `${allOpenalexCorrections.length} correction(s) to OpenAlex have been saved -
see <a href="https://github.com/dataesr/openalex-affiliations/issues" target="_blank">https://github.com/dataesr/openalex-affiliations/issues</a>`,
id: 'saveOpenAlex',
title: 'OpenAlex corrections sent',
toastType: 'success',
});
openModal();
const feedback = async () => {
try {
await sendGitHubIssue({ data: allOpenalexCorrections, email: userEmail });
toast({
description: `${allOpenalexCorrections.length} correction(s) to OpenAlex have been saved -
see <a href="https://github.com/dataesr/openalex-affiliations/issues" target="_blank">https://github.com/dataesr/openalex-affiliations/issues</a>`,
id: 'saveOpenAlex',
title: 'OpenAlex corrections sent',
toastType: 'success',
});
} catch (error) {
toast({
description: error.message,
id: 'errorOpenAlex',
title: 'Error while sending OpenAlex corrections',
toastType: 'error',
});
} finally {
switchModal();
}
};

useEffect(() => {
Expand All @@ -41,12 +51,12 @@ export default function ActionsOpenalexFeedback({ allOpenalexCorrections }) {
<>
<Button
disabled={!allOpenalexCorrections.length > 0}
onClick={openModal}
onClick={switchModal}
size="sm"
>
Send feedback to OpenAlex
</Button>
<Modal isOpen={isModalOpen} hide={openModal}>
<Modal isOpen={isModalOpen} hide={switchModal}>
<ModalTitle>
Improve OpenAlex data
</ModalTitle>
Expand Down
2 changes: 1 addition & 1 deletion client/src/utils/github.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const sendGitHubIssue = async (data) => {
headers: { 'Content-Type': 'application/json' },
}).then((response) => {
if (response.ok) return response.json();
return 'Oops... GitHub request error';
throw new Error(response.statusText);
});
};

Expand Down
7 changes: 0 additions & 7 deletions client/src/utils/templates.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ const nameTemplate = (rowData) => <span dangerouslySetInnerHTML={{ __html: rowDa

const statusTemplate = (rowData) => <Badge variant={status[rowData?.status ?? rowData]?.badgeType}>{status[rowData?.status ?? rowData]?.label}</Badge>;

const resetCorrection = (rowData, allAffiliations) => {
console.log('ttt', rowData);
const row = { rowData };
row.rowData.hasCorrection = false;
row.rowData.rorsToCorrect = rowData.rors.map((e) => e.rorId).join(';');
};

const hasCorrectionTemplate = (rowData) => (rowData?.hasCorrection
? (
<Badge variant={correction.corrected.badgeType}>{correction.corrected.label}</Badge>
Expand Down
11 changes: 6 additions & 5 deletions server/src/routes/github.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const createIssue = (issue, email) => {
);
},
onSecondaryRateLimit: (retryAfter, options) => {
// Retry 3 times after hitting a rate limit error, then give up
// Retry 5 times after hitting a rate limit error after 60 seconds
if (options.request.retryCount <= 5) {
return true;
}
Expand All @@ -59,7 +59,7 @@ const createIssue = (issue, email) => {
}
body += `works_examples: ${workIds}\n`;
body += `contact: ${encrypt(email.split('@')[0])} @ ${email.split('@')[1]}\n`;
octokit.rest.issues.create({
return octokit.rest.issues.create({
body,
owner: 'dataesr',
repo: 'openalex-affiliations',
Expand All @@ -71,9 +71,10 @@ router.route('/github-issue')
.post(async (req, res) => {
const data = req.body?.data || [];
const email = req.body?.email || '';
const promises = data.map((item) => createIssue(item, email));
await Promise.all(promises);
res.status(200).json({ message: 'GitHub Issues created' });
const promises = data.map((item) => createIssue(item, email).catch((error) => error));
const results = await Promise.all(promises);
const firstError = results.find((result) => !result.status.toString().startsWith('2'));
res.status(firstError?.status ?? 200).json({ message: firstError?.message ?? 'GitHub issues created' });
});

export default router;

0 comments on commit c5231eb

Please sign in to comment.