From 15f84b04c026749f88404a099df3fe840e2a13a0 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 26 May 2022 08:08:12 -0400 Subject: [PATCH] Handle errors in (most) shared code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two outliers to come back to are: - the LocalStoreManager, where we can’t set a current error in the WriteFreelyModel in methods that can’t throw - the CollectionListModel, where the initializer can’t throw because we use it as a property initializer in CollectionListView --- Shared/Extensions/WriteFreelyModel+APIHandlers.swift | 4 ++-- Shared/LocalStorageManager.swift | 6 +++--- Shared/PostCollection/CollectionListModel.swift | 2 +- Shared/PostCollection/CollectionListView.swift | 11 +++++++++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift index b6dc7f3..3163676 100644 --- a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift +++ b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift @@ -40,8 +40,8 @@ extension WriteFreelyModel { client = nil DispatchQueue.main.async { self.account.logout() - LocalStorageManager.standard.purgeUserCollections() do { + try LocalStorageManager.standard.purgeUserCollections() try self.posts.purgePublishedPosts() } catch { self.currentError = error @@ -59,8 +59,8 @@ extension WriteFreelyModel { client = nil DispatchQueue.main.async { self.account.logout() - LocalStorageManager.standard.purgeUserCollections() do { + try LocalStorageManager.standard.purgeUserCollections() try self.posts.purgePublishedPosts() } catch { self.currentError = error diff --git a/Shared/LocalStorageManager.swift b/Shared/LocalStorageManager.swift index af62660..ae074b4 100644 --- a/Shared/LocalStorageManager.swift +++ b/Shared/LocalStorageManager.swift @@ -23,19 +23,19 @@ final class LocalStorageManager { do { try container.viewContext.save() } catch { - print(LocalStoreError.couldNotSaveContext.localizedDescription) + fatalError(LocalStoreError.couldNotSaveContext.localizedDescription) } } } - func purgeUserCollections() { + func purgeUserCollections() throws { let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: "WFACollection") let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) do { try container.viewContext.executeAndMergeChanges(using: deleteRequest) } catch { - print(LocalStoreError.couldNotPurgeCollections.localizedDescription) + throw LocalStoreError.couldNotPurgeCollections } } diff --git a/Shared/PostCollection/CollectionListModel.swift b/Shared/PostCollection/CollectionListModel.swift index 86e4088..b2ac884 100644 --- a/Shared/PostCollection/CollectionListModel.swift +++ b/Shared/PostCollection/CollectionListModel.swift @@ -20,7 +20,7 @@ class CollectionListModel: NSObject, ObservableObject { list = collectionsController.fetchedObjects ?? [] } catch { // FIXME: Errors cannot be thrown out of the CollectionListView property initializer - print("Failed to fetch collections!") + fatalError(LocalStoreError.couldNotFetchCollections.localizedDescription) } } } diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index 29e84b1..0975fff 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -2,6 +2,7 @@ import SwiftUI struct CollectionListView: View { @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling @ObservedObject var collections = CollectionListModel( managedObjectContext: LocalStorageManager.standard.container.viewContext ) @@ -40,6 +41,16 @@ struct CollectionListView: View { self.model.editor.showAllPostsFlag = model.showAllPosts } } + .onChange(of: model.hasError) { value in + if value { + if let error = model.currentError { + self.errorHandling.handle(error: error) + } else { + self.errorHandling.handle(error: AppError.genericError()) + } + model.hasError = false + } + } } }