Skip to content

Commit 45dd691

Browse files
committed
add trycatch wrapper to fetchXml
1 parent 00b026e commit 45dd691

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/lib/api/fetchRSS.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
1919
const allItems: RSSItem[][] = []
2020
for (const url of urls) {
2121
const response = (await fetchXml(url)) as RSSResult | AtomResult
22+
2223
if ("rss" in response) {
2324
const [mainChannel] = response.rss.channel as RSSChannel[]
2425
const [source] = mainChannel.title
@@ -124,15 +125,19 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
124125
* @returns A promise that resolves to the parsed XML data as a JSON object.
125126
*/
126127
export const fetchXml = async (url: string) => {
127-
const response = await fetch(url)
128-
const xml = await response.text()
129-
let returnObject: Record<string, unknown> = {}
130-
parseString(xml, (err, result) => {
131-
if (err) {
132-
console.error(err)
133-
return
134-
}
135-
returnObject = result
136-
})
137-
return returnObject
128+
try {
129+
const response = await fetch(url)
130+
const xml = await response.text()
131+
let returnObject: Record<string, unknown> = {}
132+
parseString(xml, (err, result) => {
133+
if (err) {
134+
throw err // Throw the error to be caught by the outer try-catch
135+
}
136+
returnObject = result
137+
})
138+
return returnObject
139+
} catch (error) {
140+
console.error("Error fetching or parsing XML:", url, error)
141+
throw error
142+
}
138143
}

0 commit comments

Comments
 (0)