Skip to content

Commit

Permalink
Merge pull request #95 from koddsson/dont-crash-if-fetch-fails
Browse files Browse the repository at this point in the history
Catch any fetch error in loop
  • Loading branch information
koddsson authored Jan 12, 2024
2 parents e96732f + 7a1f81e commit 8f2326a
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,29 @@ async function getSubscriptions() {

for (const sub of subscriptions) {
if (!cache[sub.url]) {
const response = await fetch(sub.url)
const text = await response.text()
cache[sub.url] = {text, date: new Date()}
}

if (cache[sub.url]) {
try {
const response = await fetch(sub.url)
const text = await response.text()
cache[sub.url] = {text, date: new Date()}
} catch (error) {
console.error(`Failed to fetch ${sub.url}`, error)
continue;
}
} else {
const then = new Date(cache[sub.url].date).getTime()
const now = new Date().getTime()
const oneHour = 60 * 60 * 1000
const moreThanOneHourAgo = now - then > oneHour

if (moreThanOneHourAgo) {
const response = await fetch(sub.url)
const text = await response.text()
cache[sub.url] = {text, date: new Date()}
try {
const response = await fetch(sub.url)
const text = await response.text()
cache[sub.url] = {text, date: new Date()}
} catch (error) {
console.error(`Failed to fetch`, error)
continue;
}
}
}

Expand Down

0 comments on commit 8f2326a

Please sign in to comment.