diff --git a/Shared/Timeline/FetchRequestOperation.swift b/Shared/Timeline/FetchRequestOperation.swift index c9708fe0e..f679cf599 100644 --- a/Shared/Timeline/FetchRequestOperation.swift +++ b/Shared/Timeline/FetchRequestOperation.swift @@ -7,9 +7,12 @@ // import Foundation +import os import RSCore +import RSDatabase import Account import Articles +import ErrorLog // Main thread only. // Runs an asynchronous fetch. @@ -65,6 +68,8 @@ typealias FetchRequestOperationResultBlock = (Set
, FetchRequestOperatio return } + Self.logger.debug("FetchRequestOperation \(self.id, privacy: .public): run starting — \(self.fetchers.count) fetcher(s)") + let numberOfFetchers = fetchers.count var fetchersReturned = 0 var fetchedArticles = Set
() @@ -88,16 +93,24 @@ typealias FetchRequestOperationResultBlock = (Set
, FetchRequestOperatio } for fetcher in fetchers { - if (fetcher as? SidebarItem)?.readFiltered(readFilterEnabledTable: readFilterEnabledTable) ?? true { - if let articles = try? await fetcher.fetchUnreadArticlesAsync() { - process(articles) - } - } else { - if let articles = try? await fetcher.fetchArticlesAsync() { - process(articles) + let articles: Set
+ do { + if (fetcher as? SidebarItem)?.readFiltered(readFilterEnabledTable: readFilterEnabledTable) ?? true { + articles = try await fetcher.fetchUnreadArticlesAsync() + } else { + articles = try await fetcher.fetchArticlesAsync() } + } catch { + Self.logger.error("FetchRequestOperation \(self.id, privacy: .public): fetcher threw — \(String(describing: error), privacy: .public)") + Self.postFetchError(error) + articles = [] } + process(articles) } + + // Belt-and-suspenders: ensure the queue never deadlocks even if + // the loop above is ever refactored to skip process(). + callCompletionIfNeeded() } } } @@ -147,6 +160,8 @@ typealias FetchRequestOperationResultBlock = (Set
, FetchRequestOperatio return } + Self.logger.debug("FetchRequestOperation \(self.id, privacy: .public): run starting — \(self.fetchers.count) fetcher(s)") + let numberOfFetchers = fetchers.count var fetchersReturned = 0 var fetchedArticles = Set
() @@ -177,18 +192,44 @@ typealias FetchRequestOperationResultBlock = (Set
, FetchRequestOperatio } for fetcher in fetchers { - if fetcherHidesReadArticles(fetcher) { - if let articles = try? await fetcher.fetchUnreadArticlesAsync() { - process(articles) - } - } else { - if let articles = try? await fetcher.fetchArticlesAsync() { - process(articles) + let articles: Set
+ do { + if fetcherHidesReadArticles(fetcher) { + articles = try await fetcher.fetchUnreadArticlesAsync() + } else { + articles = try await fetcher.fetchArticlesAsync() } + } catch { + Self.logger.error("FetchRequestOperation \(self.id, privacy: .public): fetcher threw — \(String(describing: error), privacy: .public)") + Self.postFetchError(error) + articles = [] } + process(articles) } + + // Ensure the queue never deadlocks even if + // the loop above is ever refactored to skip process(). + callCompletionIfNeeded() } } } #endif + +private extension FetchRequestOperation { + + static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FetchRequestOperation") + static let errorLogSourceID = 101 + + static func postFetchError(_ error: Error, fileName: String = #fileID, functionName: String = #function, lineNumber: Int = #line) { + let typeName = String(describing: type(of: error)) + let description: String + if let databaseError = error as? DatabaseError { + description = "\(typeName).\(databaseError): \(error.localizedDescription)" + } else { + description = "\(typeName): \(error.localizedDescription)" + } + let userInfo = ErrorLogUserInfoKey.userInfo(sourceName: "Timeline", sourceID: errorLogSourceID, operation: "Fetching articles", errorMessage: description, fileName: fileName, functionName: functionName, lineNumber: lineNumber) + NotificationCenter.default.post(name: .appDidEncounterError, object: nil, userInfo: userInfo) + } +}