Skip to content

Commit

Permalink
add trycatch wrapper to fetchXml
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Oct 15, 2024
1 parent 00b026e commit 45dd691
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/lib/api/fetchRSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
const allItems: RSSItem[][] = []
for (const url of urls) {
const response = (await fetchXml(url)) as RSSResult | AtomResult

if ("rss" in response) {
const [mainChannel] = response.rss.channel as RSSChannel[]
const [source] = mainChannel.title
Expand Down Expand Up @@ -124,15 +125,19 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
* @returns A promise that resolves to the parsed XML data as a JSON object.
*/
export const fetchXml = async (url: string) => {
const response = await fetch(url)
const xml = await response.text()
let returnObject: Record<string, unknown> = {}
parseString(xml, (err, result) => {
if (err) {
console.error(err)
return
}
returnObject = result
})
return returnObject
try {
const response = await fetch(url)
const xml = await response.text()
let returnObject: Record<string, unknown> = {}
parseString(xml, (err, result) => {
if (err) {
throw err // Throw the error to be caught by the outer try-catch
}
returnObject = result
})
return returnObject
} catch (error) {
console.error("Error fetching or parsing XML:", url, error)
throw error
}
}

0 comments on commit 45dd691

Please sign in to comment.