Handle network requests failing more gracefully

This commit is contained in:
pfych 2025-04-30 19:26:25 +10:00
parent 040b427d97
commit 9eb25f706e

View File

@ -22,34 +22,39 @@ void (async () => {
await getMangaToFetch(config); await getMangaToFetch(config);
for (const mangaId of uniqueMangaIds) { for (const mangaId of uniqueMangaIds) {
const lastChapterId = await mangaHistory.get<ChapterId>(mangaId); try {
const lastChapterId = await mangaHistory.get<ChapterId>(mangaId);
const manga = await getManga(mangaId, userAgent); const manga = await getManga(mangaId, userAgent);
const chapters = await getAllChapters(mangaId, userAgent); const chapters = await getAllChapters(mangaId, userAgent);
const latestChapter = getLatestChapter(chapters); const latestChapter = getLatestChapter(chapters);
const cover = await getCover(manga, userAgent); const cover = await getCover(manga, userAgent);
const title = getMangaTitle(manga); const title = getMangaTitle(manga);
if (lastChapterId !== latestChapter.id) { if (lastChapterId !== latestChapter.id) {
console.log('Update found for manga:', title); console.log('Update found for manga:', title);
const webhooksForManga = mangaIdsToWebhooks[mangaId]; const webhooksForManga = mangaIdsToWebhooks[mangaId];
await Promise.all( await Promise.all(
webhooksForManga.map( webhooksForManga.map(
async (webhookUrl) => async (webhookUrl) =>
await sendWebhook({ await sendWebhook({
webhookUrl, webhookUrl,
manga, manga,
latestChapter, latestChapter,
cover, cover,
}), }),
), ),
); );
await mangaHistory.set(mangaId, latestChapter.id); await mangaHistory.set(mangaId, latestChapter.id);
} else { } else {
console.log('No Updates found for manga:', title); console.log('No Updates found for manga:', title);
}
} catch (err) {
console.error(`Failed to fetch ${mangaId}!`);
console.error(err);
} }
} }
}; };