Handle purging post errors

This commit is contained in:
Angelo Stavrow 2022-05-23 15:52:20 -04:00
parent 11d2e41ab5
commit b017e21e06
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 19 additions and 7 deletions

View File

@ -102,7 +102,7 @@ enum LocalStoreError: Error {
case couldNotSaveContext case couldNotSaveContext
case couldNotFetchCollections case couldNotFetchCollections
case couldNotFetchPosts(String = "") case couldNotFetchPosts(String = "")
case couldNotPurgePublishedPosts case couldNotPurgePosts(String = "")
case couldNotPurgeCollections case couldNotPurgeCollections
case couldNotLoadStore(String) case couldNotLoadStore(String)
case couldNotMigrateStore(String) case couldNotMigrateStore(String)
@ -123,8 +123,12 @@ extension LocalStoreError: LocalizedError {
} else { } else {
return NSLocalizedString("Failed to fetch \(postFilter) posts from local store.", comment: "") return NSLocalizedString("Failed to fetch \(postFilter) posts from local store.", comment: "")
} }
case .couldNotPurgePublishedPosts: case .couldNotPurgePosts(let postFilter):
return NSLocalizedString("Failed to purge published posts from local store.", comment: "") if postFilter.isEmpty {
return NSLocalizedString("Failed to purge \(postFilter) posts from local store.", comment: "")
} else {
return NSLocalizedString("Failed to purge posts from local store.", comment: "")
}
case .couldNotPurgeCollections: case .couldNotPurgeCollections:
return NSLocalizedString("Failed to purge cached collections", comment: "") return NSLocalizedString("Failed to purge cached collections", comment: "")
case .couldNotLoadStore(let errorDescription): case .couldNotLoadStore(let errorDescription):

View File

@ -41,7 +41,11 @@ extension WriteFreelyModel {
DispatchQueue.main.async { DispatchQueue.main.async {
self.account.logout() self.account.logout()
LocalStorageManager.standard.purgeUserCollections() LocalStorageManager.standard.purgeUserCollections()
self.posts.purgePublishedPosts() do {
try self.posts.purgePublishedPosts()
} catch {
self.currentError = error
}
} }
} catch { } catch {
self.currentError = KeychainError.couldNotPurgeAccessToken self.currentError = KeychainError.couldNotPurgeAccessToken
@ -56,7 +60,11 @@ extension WriteFreelyModel {
DispatchQueue.main.async { DispatchQueue.main.async {
self.account.logout() self.account.logout()
LocalStorageManager.standard.purgeUserCollections() LocalStorageManager.standard.purgeUserCollections()
self.posts.purgePublishedPosts() do {
try self.posts.purgePublishedPosts()
} catch {
self.currentError = error
}
} }
} catch { } catch {
self.currentError = KeychainError.couldNotPurgeAccessToken self.currentError = KeychainError.couldNotPurgeAccessToken

View File

@ -9,7 +9,7 @@ class PostListModel: ObservableObject {
} }
} }
func purgePublishedPosts() { func purgePublishedPosts() throws {
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFAPost") let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFAPost")
fetchRequest.predicate = NSPredicate(format: "status != %i", 0) fetchRequest.predicate = NSPredicate(format: "status != %i", 0)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
@ -17,7 +17,7 @@ class PostListModel: ObservableObject {
do { do {
try LocalStorageManager.standard.container.viewContext.executeAndMergeChanges(using: deleteRequest) try LocalStorageManager.standard.container.viewContext.executeAndMergeChanges(using: deleteRequest)
} catch { } catch {
print("Error: Failed to purge cached posts.") throw LocalStoreError.couldNotPurgePosts("cached")
} }
} }