mirror of
https://github.com/Ferrite-iOS/Ferrite.git
synced 2026-05-09 19:50:32 +00:00
Scraping: Add source specific errors
State what source errored when one occurs. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
parent
e063b91f3f
commit
102b59ab0a
1 changed files with 21 additions and 20 deletions
|
|
@ -29,6 +29,17 @@ class ScrapingViewModel: ObservableObject {
|
||||||
searchResults = newResults
|
searchResults = newResults
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Utility function to print source specific errors
|
||||||
|
func sendSourceError(_ description: String, newToastType: ToastViewModel.ToastType? = nil) async {
|
||||||
|
let newDescription = "\(currentSourceName ?? "No source given"): \(description)"
|
||||||
|
await toastModel?.updateToastDescription(
|
||||||
|
newDescription,
|
||||||
|
newToastType: newToastType
|
||||||
|
)
|
||||||
|
|
||||||
|
print(newDescription)
|
||||||
|
}
|
||||||
|
|
||||||
public func scanSources(sources: [Source]) async {
|
public func scanSources(sources: [Source]) async {
|
||||||
if sources.isEmpty {
|
if sources.isEmpty {
|
||||||
await toastModel?.updateToastDescription("There are no sources to search!", newToastType: .info)
|
await toastModel?.updateToastDescription("There are no sources to search!", newToastType: .info)
|
||||||
|
|
@ -56,8 +67,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
let preferredParser = SourcePreferredParser(rawValue: source.preferredParser) ?? .none
|
let preferredParser = SourcePreferredParser(rawValue: source.preferredParser) ?? .none
|
||||||
|
|
||||||
guard let encodedQuery = searchText.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
|
guard let encodedQuery = searchText.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
|
||||||
await toastModel?.updateToastDescription("Could not process search query, invalid characters present.")
|
await sendSourceError("Could not process search query, invalid characters present.")
|
||||||
print("Could not process search query, invalid characters present")
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -255,13 +265,11 @@ class ScrapingViewModel: ObservableObject {
|
||||||
case -999:
|
case -999:
|
||||||
await toastModel?.updateToastDescription("Search cancelled", newToastType: .info)
|
await toastModel?.updateToastDescription("Search cancelled", newToastType: .info)
|
||||||
case -1001:
|
case -1001:
|
||||||
await toastModel?.updateToastDescription("Credentials request timed out")
|
await sendSourceError("Credentials request timed out")
|
||||||
default:
|
default:
|
||||||
await toastModel?.updateToastDescription("Error in fetching an API credential \(error)")
|
await sendSourceError("Error in fetching an API credential \(error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Error in fetching an API credential \(error)")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -269,9 +277,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
// Fetches the data for a URL
|
// Fetches the data for a URL
|
||||||
public func fetchWebsiteData(urlString: String) async -> Data? {
|
public func fetchWebsiteData(urlString: String) async -> Data? {
|
||||||
guard let url = URL(string: urlString) else {
|
guard let url = URL(string: urlString) else {
|
||||||
await toastModel?.updateToastDescription("Source doesn't contain a valid URL, contact the source dev!")
|
await sendSourceError("Source doesn't contain a valid URL, contact the source dev!")
|
||||||
|
|
||||||
print("Source doesn't contain a valid URL, contact the source dev!")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -288,13 +294,11 @@ class ScrapingViewModel: ObservableObject {
|
||||||
case -999:
|
case -999:
|
||||||
await toastModel?.updateToastDescription("Search cancelled", newToastType: .info)
|
await toastModel?.updateToastDescription("Search cancelled", newToastType: .info)
|
||||||
case -1001:
|
case -1001:
|
||||||
await toastModel?.updateToastDescription("Data request timed out. Trying fallback URLs if present.")
|
await sendSourceError("Data request timed out. Trying fallback URLs if present.")
|
||||||
default:
|
default:
|
||||||
await toastModel?.updateToastDescription("Error in fetching website data \(error)")
|
await sendSourceError("Error in fetching website data \(error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Error in fetching data \(error)")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -465,8 +469,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
let document = try SwiftSoup.parse(rss, "", Parser.xmlParser())
|
let document = try SwiftSoup.parse(rss, "", Parser.xmlParser())
|
||||||
items = try document.getElementsByTag(rssParser.items)
|
items = try document.getElementsByTag(rssParser.items)
|
||||||
} catch {
|
} catch {
|
||||||
await toastModel?.updateToastDescription("RSS scraping error, couldn't fetch items: \(error)")
|
await sendSourceError("RSS scraping error, couldn't fetch items: \(error)")
|
||||||
print("RSS scraping error, couldn't fetch items: \(error)")
|
|
||||||
|
|
||||||
return tempResults
|
return tempResults
|
||||||
}
|
}
|
||||||
|
|
@ -620,8 +623,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
let document = try SwiftSoup.parse(html)
|
let document = try SwiftSoup.parse(html)
|
||||||
rows = try document.select(htmlParser.rows)
|
rows = try document.select(htmlParser.rows)
|
||||||
} catch {
|
} catch {
|
||||||
await toastModel?.updateToastDescription("Scraping error, couldn't fetch rows: \(error)")
|
await sendSourceError("Scraping error, couldn't fetch rows: \(error)")
|
||||||
print("Scraping error, couldn't fetch rows: \(error)")
|
|
||||||
|
|
||||||
return tempResults
|
return tempResults
|
||||||
}
|
}
|
||||||
|
|
@ -753,8 +755,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
tempResults.append(result)
|
tempResults.append(result)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
await toastModel?.updateToastDescription("Scraping error: \(error)")
|
await sendSourceError("Scraping error: \(error)")
|
||||||
print("Scraping error: \(error)")
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -901,7 +902,7 @@ class ScrapingViewModel: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await toastModel?.updateToastDescription(responseArray.joined(separator: " "))
|
await sendSourceError(responseArray.joined(separator: " "))
|
||||||
|
|
||||||
PersistenceController.shared.save(backgroundContext)
|
PersistenceController.shared.save(backgroundContext)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue