From b017e21e066dbcf86a3166fc06d19222047b78b9 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Mon, 23 May 2022 15:52:20 -0400 Subject: [PATCH] Handle purging post errors --- Shared/ErrorHandling/ErrorConstants.swift | 10 +++++++--- Shared/Extensions/WriteFreelyModel+APIHandlers.swift | 12 ++++++++++-- Shared/PostList/PostListModel.swift | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Shared/ErrorHandling/ErrorConstants.swift b/Shared/ErrorHandling/ErrorConstants.swift index b412fdb..88ae92b 100644 --- a/Shared/ErrorHandling/ErrorConstants.swift +++ b/Shared/ErrorHandling/ErrorConstants.swift @@ -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): diff --git a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift index c1d19d9..b6dc7f3 100644 --- a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift +++ b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift @@ -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 diff --git a/Shared/PostList/PostListModel.swift b/Shared/PostList/PostListModel.swift index db0ff4a..edd545c 100644 --- a/Shared/PostList/PostListModel.swift +++ b/Shared/PostList/PostListModel.swift @@ -9,7 +9,7 @@ class PostListModel: ObservableObject { } } - func purgePublishedPosts() { + func purgePublishedPosts() throws { let fetchRequest: NSFetchRequest = 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") } }