Properly handle errors when trying to read server url in FileProviderEnumerator

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-02-02 14:18:46 +01:00
parent 2f12d9b2bd
commit 6a258c0c37
No known key found for this signature in database
GPG key ID: C839200C384636B0

View file

@ -76,8 +76,8 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage { page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {
NSLog("Enumerating initial page for user: %@ with serverUrl: %@", ncAccount.username, serverUrl) NSLog("Enumerating initial page for user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount) { metadatas in FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount) { metadatas, readError in
FileProviderEnumerator.completeObserver(observer, numPage: 1, itemMetadatas: metadatas) FileProviderEnumerator.completeObserver(observer, numPage: 1, itemMetadatas: metadatas, error: readError)
} }
return; return;
@ -85,7 +85,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)! let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!
NSLog("Enumerating page %d for user: %@ with serverUrl: %@", numPage, ncAccount.username, serverUrl) NSLog("Enumerating page %d for user: %@ with serverUrl: %@", numPage, ncAccount.username, serverUrl)
FileProviderEnumerator.completeObserver(observer, numPage: numPage, itemMetadatas: nil) FileProviderEnumerator.completeObserver(observer, numPage: numPage, itemMetadatas: nil, error: nil)
} }
func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) { func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
@ -108,12 +108,17 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
// MARK: - Helper methods // MARK: - Helper methods
private static func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, itemMetadatas: [NextcloudItemMetadataTable]?) { private static func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, itemMetadatas: [NextcloudItemMetadataTable]?, error: Error?) {
guard error == nil else {
NSLog("Finishing enumeration with error")
observer.finishEnumeratingWithError(error!)
return;
}
guard itemMetadatas != nil else { guard itemMetadatas != nil else {
NSLog("Received nil metadatas, finish empty enumeration") NSLog("Received nil metadatas, finish empty enumeration")
observer.finishEnumerating(upTo: nil) observer.finishEnumerating(upTo: nil)
return return
} }
var items: [NSFileProviderItem] = [] var items: [NSFileProviderItem] = []
@ -147,14 +152,14 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
} }
} }
private static func finishReadServerUrl(_ serverUrlPath: String, ncKitAccount: String, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?) -> Void) { private static func finishReadServerUrl(_ serverUrlPath: String, ncKitAccount: String, readError: Error?, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?, _ readError: Error?) -> Void) {
let metadatas = NextcloudFilesDatabaseManager.shared.itemMetadatas(account: ncKitAccount, serverUrl: serverUrlPath) let metadatas = NextcloudFilesDatabaseManager.shared.itemMetadatas(account: ncKitAccount, serverUrl: serverUrlPath)
NSLog("Finished reading serverUrl: %@ for user: %@. Processed %d metadatas", serverUrlPath, ncKitAccount, metadatas.count) NSLog("Finished reading serverUrl: %@ for user: %@. Processed %d metadatas", serverUrlPath, ncKitAccount, metadatas.count)
completionHandler(metadatas) completionHandler(metadatas, readError)
} }
private static func readServerUrl(_ serverUrl: String, ncAccount: NextcloudAccount, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?) -> Void) { private static func readServerUrl(_ serverUrl: String, ncAccount: NextcloudAccount, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?, _ readError: Error?) -> Void) {
let dbManager = NextcloudFilesDatabaseManager.shared let dbManager = NextcloudFilesDatabaseManager.shared
let ncKitAccount = ncAccount.ncKitAccount let ncKitAccount = ncAccount.ncKitAccount
var directoryEtag: String? var directoryEtag: String?
@ -168,14 +173,14 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: true) { account, files, _, error in NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: true) { account, files, _, error in
guard error == .success else { guard error == .success else {
NSLog("0 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription) NSLog("0 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler) finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, readError: error.error, completionHandler: completionHandler)
return return
} }
// If we have already done a 0 depth scan of this folder then we might get matching etag // If we have already done a 0 depth scan of this folder then we might get matching etag
guard directoryEtag != files.first?.etag else { guard directoryEtag != files.first?.etag else {
NSLog("Fetched directory etag is same as that stored locally (serverUrl: %@ user: %@). Not fetching child items.", serverUrl, account) NSLog("Fetched directory etag is same as that stored locally (serverUrl: %@ user: %@). Not fetching child items.", serverUrl, account)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler) finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, readError: nil, completionHandler: completionHandler)
return return
} }
@ -184,7 +189,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: true) { account, files, _, error in NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: true) { account, files, _, error in
guard error == .success else { guard error == .success else {
NSLog("1 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription) NSLog("1 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler) finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, readError: error.error, completionHandler: completionHandler)
return return
} }
@ -200,7 +205,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
// Since we haven't scanned the contents of these, don't record their itemMetadata etags in the directory tables // Since we haven't scanned the contents of these, don't record their itemMetadata etags in the directory tables
dbManager.updateDirectoryMetadatasFromItemMetadatas(account: ncKitAccount, parentDirectoryServerUrl: serverUrl, updatedDirectoryItemMetadatas: childDirectoriesMetadata) dbManager.updateDirectoryMetadatasFromItemMetadatas(account: ncKitAccount, parentDirectoryServerUrl: serverUrl, updatedDirectoryItemMetadatas: childDirectoriesMetadata)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler) finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, readError: nil, completionHandler: completionHandler)
} }
} }
} }