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 couldNotFetchCollections
case couldNotFetchPosts(String = "")
case couldNotPurgePublishedPosts
case couldNotPurgePosts(String = "")
case couldNotPurgeCollections
case couldNotLoadStore(String)
case couldNotMigrateStore(String)
@ -123,8 +123,12 @@ extension LocalStoreError: LocalizedError {
} else {
return NSLocalizedString("Failed to fetch \(postFilter) posts from local store.", comment: "")
}
case .couldNotPurgePublishedPosts:
return NSLocalizedString("Failed to purge published posts from local store.", comment: "")
case .couldNotPurgePosts(let postFilter):
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:
return NSLocalizedString("Failed to purge cached collections", comment: "")
case .couldNotLoadStore(let errorDescription):

View File

@ -41,7 +41,11 @@ extension WriteFreelyModel {
DispatchQueue.main.async {
self.account.logout()
LocalStorageManager.standard.purgeUserCollections()
self.posts.purgePublishedPosts()
do {
try self.posts.purgePublishedPosts()
} catch {
self.currentError = error
}
}
} catch {
self.currentError = KeychainError.couldNotPurgeAccessToken
@ -56,7 +60,11 @@ extension WriteFreelyModel {
DispatchQueue.main.async {
self.account.logout()
LocalStorageManager.standard.purgeUserCollections()
self.posts.purgePublishedPosts()
do {
try self.posts.purgePublishedPosts()
} catch {
self.currentError = error
}
}
} catch {
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")
fetchRequest.predicate = NSPredicate(format: "status != %i", 0)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
@ -17,7 +17,7 @@ class PostListModel: ObservableObject {
do {
try LocalStorageManager.standard.container.viewContext.executeAndMergeChanges(using: deleteRequest)
} catch {
print("Error: Failed to purge cached posts.")
throw LocalStoreError.couldNotPurgePosts("cached")
}
}