Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch errors on xml fetch #14168

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading