From 9eb25f706e7c1ed5c3e9054e1d3feaa81e9b4758 Mon Sep 17 00:00:00 2001 From: pfych Date: Wed, 30 Apr 2025 19:26:25 +1000 Subject: [PATCH] Handle network requests failing more gracefully --- src/index.ts | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index b9f97da..58b6cae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,34 +22,39 @@ void (async () => { await getMangaToFetch(config); for (const mangaId of uniqueMangaIds) { - const lastChapterId = await mangaHistory.get(mangaId); + try { + const lastChapterId = await mangaHistory.get(mangaId); - const manga = await getManga(mangaId, userAgent); - const chapters = await getAllChapters(mangaId, userAgent); - const latestChapter = getLatestChapter(chapters); - const cover = await getCover(manga, userAgent); - const title = getMangaTitle(manga); + const manga = await getManga(mangaId, userAgent); + const chapters = await getAllChapters(mangaId, userAgent); + const latestChapter = getLatestChapter(chapters); + const cover = await getCover(manga, userAgent); + const title = getMangaTitle(manga); - if (lastChapterId !== latestChapter.id) { - console.log('Update found for manga:', title); + if (lastChapterId !== latestChapter.id) { + console.log('Update found for manga:', title); - const webhooksForManga = mangaIdsToWebhooks[mangaId]; + const webhooksForManga = mangaIdsToWebhooks[mangaId]; - await Promise.all( - webhooksForManga.map( - async (webhookUrl) => - await sendWebhook({ - webhookUrl, - manga, - latestChapter, - cover, - }), - ), - ); + await Promise.all( + webhooksForManga.map( + async (webhookUrl) => + await sendWebhook({ + webhookUrl, + manga, + latestChapter, + cover, + }), + ), + ); - await mangaHistory.set(mangaId, latestChapter.id); - } else { - console.log('No Updates found for manga:', title); + await mangaHistory.set(mangaId, latestChapter.id); + } else { + console.log('No Updates found for manga:', title); + } + } catch (err) { + console.error(`Failed to fetch ${mangaId}!`); + console.error(err); } } };