Skip to content

Commit

Permalink
Refactor fetchRSSFeed and fetchJson functions to handle errors and lo…
Browse files Browse the repository at this point in the history
…g them
  • Loading branch information
BumpyClock committed Apr 16, 2024
1 parent 89809f3 commit 1a50a63
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,27 @@ async function fetchRSSFeedAndUpdateCache(feedUrls) {

//Get thumbnailUrl from the feed items and cache the images
async function fetchRSSFeed(feedUrls) {
const apiUrl = await getApiUrl();
const requestUrl = `${apiUrl}/parse`;
const requestOptions = createRequestOptions(feedUrls);

console.log("fetchRSSFeed: getApiUrl" + apiUrl);

const response = await fetch(requestUrl, requestOptions);
const fetchedFeedData = await response.json();

if (response.ok) {
// Filter out feeds with a status other than "ok"
fetchedFeedData.feeds = fetchedFeedData.feeds.filter(feed => feed.status === "ok");
return fetchedFeedData;
} else {
console.error("API response: ", JSON.stringify(fetchedFeedData));
throw new Error("Failed to fetch RSS feeds");
try {
const apiUrl = await getApiUrl();
const requestUrl = `${apiUrl}/parse`;
const requestOptions = createRequestOptions(feedUrls);

console.log("fetchRSSFeed: getApiUrl" + apiUrl);

const response = await fetch(requestUrl, requestOptions);
const fetchedFeedData = await response.json();

if (response.ok) {
// Filter out feeds with a status other than "ok"
fetchedFeedData.feeds = fetchedFeedData.feeds.filter(feed => feed.status === "ok");
return fetchedFeedData;
} else {
console.error("API response: ", JSON.stringify(fetchedFeedData));
throw new Error("Failed to fetch RSS feeds");
}
} catch (error) {
console.error('An error occurred:', error);
throw error; // re-throw the error if you want it to propagate
}
}

Expand All @@ -230,11 +235,16 @@ self.addEventListener('activate', async (event) => {


async function fetchJson(url, options) {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`Failed to fetch ${url}`);
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`Failed to fetch ${url}`);
}
return response.json();
} catch (error) {
console.error('An error occurred:', error);
throw error; // re-throw the error if you want it to propagate
}
return response.json();
}

async function getCachedBingImageBlob() {
Expand Down

0 comments on commit 1a50a63

Please sign in to comment.