mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Handle errors in (most) shared code
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
This commit is contained in:
parent
c5b611b39e
commit
15f84b04c0
@ -40,8 +40,8 @@ extension WriteFreelyModel {
|
|||||||
client = nil
|
client = nil
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.account.logout()
|
self.account.logout()
|
||||||
LocalStorageManager.standard.purgeUserCollections()
|
|
||||||
do {
|
do {
|
||||||
|
try LocalStorageManager.standard.purgeUserCollections()
|
||||||
try self.posts.purgePublishedPosts()
|
try self.posts.purgePublishedPosts()
|
||||||
} catch {
|
} catch {
|
||||||
self.currentError = error
|
self.currentError = error
|
||||||
@ -59,8 +59,8 @@ extension WriteFreelyModel {
|
|||||||
client = nil
|
client = nil
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.account.logout()
|
self.account.logout()
|
||||||
LocalStorageManager.standard.purgeUserCollections()
|
|
||||||
do {
|
do {
|
||||||
|
try LocalStorageManager.standard.purgeUserCollections()
|
||||||
try self.posts.purgePublishedPosts()
|
try self.posts.purgePublishedPosts()
|
||||||
} catch {
|
} catch {
|
||||||
self.currentError = error
|
self.currentError = error
|
||||||
|
@ -23,19 +23,19 @@ final class LocalStorageManager {
|
|||||||
do {
|
do {
|
||||||
try container.viewContext.save()
|
try container.viewContext.save()
|
||||||
} catch {
|
} catch {
|
||||||
print(LocalStoreError.couldNotSaveContext.localizedDescription)
|
fatalError(LocalStoreError.couldNotSaveContext.localizedDescription)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func purgeUserCollections() {
|
func purgeUserCollections() throws {
|
||||||
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFACollection")
|
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFACollection")
|
||||||
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
|
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try container.viewContext.executeAndMergeChanges(using: deleteRequest)
|
try container.viewContext.executeAndMergeChanges(using: deleteRequest)
|
||||||
} catch {
|
} catch {
|
||||||
print(LocalStoreError.couldNotPurgeCollections.localizedDescription)
|
throw LocalStoreError.couldNotPurgeCollections
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ class CollectionListModel: NSObject, ObservableObject {
|
|||||||
list = collectionsController.fetchedObjects ?? []
|
list = collectionsController.fetchedObjects ?? []
|
||||||
} catch {
|
} catch {
|
||||||
// FIXME: Errors cannot be thrown out of the CollectionListView property initializer
|
// FIXME: Errors cannot be thrown out of the CollectionListView property initializer
|
||||||
print("Failed to fetch collections!")
|
fatalError(LocalStoreError.couldNotFetchCollections.localizedDescription)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import SwiftUI
|
|||||||
|
|
||||||
struct CollectionListView: View {
|
struct CollectionListView: View {
|
||||||
@EnvironmentObject var model: WriteFreelyModel
|
@EnvironmentObject var model: WriteFreelyModel
|
||||||
|
@EnvironmentObject var errorHandling: ErrorHandling
|
||||||
@ObservedObject var collections = CollectionListModel(
|
@ObservedObject var collections = CollectionListModel(
|
||||||
managedObjectContext: LocalStorageManager.standard.container.viewContext
|
managedObjectContext: LocalStorageManager.standard.container.viewContext
|
||||||
)
|
)
|
||||||
@ -40,6 +41,16 @@ struct CollectionListView: View {
|
|||||||
self.model.editor.showAllPostsFlag = model.showAllPosts
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user