Revert "Log fatal errors and present alert on next launch (#212)"

This reverts commit 7475b57772.
This commit is contained in:
Angelo Stavrow 2022-07-27 11:42:05 -04:00
parent 7475b57772
commit f1b0a20643
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
6 changed files with 7 additions and 127 deletions

View File

@ -13,8 +13,6 @@ enum WFDefaults {
static let automaticallyChecksForUpdates = "automaticallyChecksForUpdates" static let automaticallyChecksForUpdates = "automaticallyChecksForUpdates"
static let subscribeToBetaUpdates = "subscribeToBetaUpdates" static let subscribeToBetaUpdates = "subscribeToBetaUpdates"
#endif #endif
static let didHaveFatalError = "didHaveFatalError"
static let fatalErrorDescription = "fatalErrorDescription"
} }
extension UserDefaults { extension UserDefaults {

View File

@ -8,8 +8,6 @@ import AppKit
final class LocalStorageManager { final class LocalStorageManager {
private let logger = Logging(for: String(describing: LocalStorageManager.self))
public static var standard = LocalStorageManager() public static var standard = LocalStorageManager()
public let container: NSPersistentContainer public let container: NSPersistentContainer
private let containerName = "LocalStorageModel" private let containerName = "LocalStorageModel"
@ -23,11 +21,9 @@ final class LocalStorageManager {
func saveContext() { func saveContext() {
if container.viewContext.hasChanges { if container.viewContext.hasChanges {
do { do {
logger.log("Saving context to local store started...")
try container.viewContext.save() try container.viewContext.save()
logger.log("Context saved to local store.")
} catch { } catch {
logger.logCrashAndSetFlag(error: LocalStoreError.couldNotSaveContext) fatalError(LocalStoreError.couldNotSaveContext.localizedDescription)
} }
} }
} }
@ -37,11 +33,8 @@ final class LocalStorageManager {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do { do {
logger.log("Purging user collections from local store...")
try container.viewContext.executeAndMergeChanges(using: deleteRequest) try container.viewContext.executeAndMergeChanges(using: deleteRequest)
logger.log("User collections purged from local store.")
} catch { } catch {
logger.log("\(LocalStoreError.couldNotPurgeCollections.localizedDescription)", level: .error)
throw LocalStoreError.couldNotPurgeCollections throw LocalStoreError.couldNotPurgeCollections
} }
} }
@ -67,11 +60,9 @@ private extension LocalStorageManager {
} }
container.loadPersistentStores { _, error in container.loadPersistentStores { _, error in
self.logger.log("Loading local store...")
if let error = error { if let error = error {
self.logger.logCrashAndSetFlag(error: LocalStoreError.couldNotLoadStore(error.localizedDescription)) fatalError(LocalStoreError.couldNotLoadStore(error.localizedDescription).localizedDescription)
} }
self.logger.log("Loaded local store.")
} }
migrateStore(for: container) migrateStore(for: container)
container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.automaticallyMergesChangesFromParent = true
@ -92,24 +83,20 @@ private extension LocalStorageManager {
// Attempt to migrate the old store over to the shared store URL. // Attempt to migrate the old store over to the shared store URL.
do { do {
self.logger.log("Migrating local store to shared store...")
try coordinator.migratePersistentStore(oldStore, try coordinator.migratePersistentStore(oldStore,
to: sharedStoreURL, to: sharedStoreURL,
options: nil, options: nil,
withType: NSSQLiteStoreType) withType: NSSQLiteStoreType)
self.logger.log("Migrated local store to shared store.")
} catch { } catch {
logger.logCrashAndSetFlag(error: LocalStoreError.couldNotMigrateStore(error.localizedDescription)) fatalError(LocalStoreError.couldNotMigrateStore(error.localizedDescription).localizedDescription)
} }
// Attempt to delete the old store. // Attempt to delete the old store.
do { do {
logger.log("Deleting migrated local store...")
try FileManager.default.removeItem(at: oldStoreURL) try FileManager.default.removeItem(at: oldStoreURL)
logger.log("Deleted migrated local store.")
} catch { } catch {
logger.logCrashAndSetFlag( fatalError(
error: LocalStoreError.couldNotDeleteStoreAfterMigration(error.localizedDescription) LocalStoreError.couldNotDeleteStoreAfterMigration(error.localizedDescription).localizedDescription
) )
} }
} }

View File

@ -1,50 +0,0 @@
//
// Logging.swift
// WriteFreely-MultiPlatform
//
// Created by Angelo Stavrow on 2022-06-25.
//
import Foundation
import os
import OSLog
protocol LogWriter {
func log(_ message: String, withSensitiveInfo privateInfo: String?, level: OSLogType)
func logCrashAndSetFlag(error: Error)
}
final class Logging {
private let logger: Logger
private let subsystem = Bundle.main.bundleIdentifier!
init(for category: String = "") {
self.logger = Logger(subsystem: subsystem, category: category)
}
}
extension Logging: LogWriter {
func log(
_ message: String,
withSensitiveInfo privateInfo: String? = nil,
level: OSLogType = .default
) {
if let privateInfo = privateInfo {
logger.log(level: level, "\(message): \(privateInfo, privacy: .sensitive)")
} else {
logger.log(level: level, "\(message)")
}
}
func logCrashAndSetFlag(error: Error) {
let errorDescription = error.localizedDescription
UserDefaults.shared.set(true, forKey: WFDefaults.didHaveFatalError)
UserDefaults.shared.set(errorDescription, forKey: WFDefaults.fatalErrorDescription)
logger.log(level: .error, "\(errorDescription)")
fatalError(errorDescription)
}
}

View File

@ -2,9 +2,6 @@ import SwiftUI
import CoreData import CoreData
class CollectionListModel: NSObject, ObservableObject { class CollectionListModel: NSObject, ObservableObject {
private let logger = Logging(for: String(describing: CollectionListModel.self))
@Published var list: [WFACollection] = [] @Published var list: [WFACollection] = []
private let collectionsController: NSFetchedResultsController<WFACollection> private let collectionsController: NSFetchedResultsController<WFACollection>
@ -19,12 +16,11 @@ class CollectionListModel: NSObject, ObservableObject {
collectionsController.delegate = self collectionsController.delegate = self
do { do {
logger.log("Fetching collections from local store...")
try collectionsController.performFetch() try collectionsController.performFetch()
list = collectionsController.fetchedObjects ?? [] list = collectionsController.fetchedObjects ?? []
logger.log("Fetched collections from local store.")
} catch { } catch {
logger.logCrashAndSetFlag(error: LocalStoreError.couldNotFetchCollections) // FIXME: Errors cannot be thrown out of the CollectionListView property initializer
fatalError(LocalStoreError.couldNotFetchCollections.localizedDescription)
} }
} }
} }

View File

@ -30,8 +30,6 @@ struct WriteFreely_MultiPlatformApp: App {
@State private var selectedTab = 0 @State private var selectedTab = 0
#endif #endif
@State private var didCrash = UserDefaults.shared.bool(forKey: WFDefaults.didHaveFatalError)
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
ContentView() ContentView()
@ -50,24 +48,6 @@ struct WriteFreely_MultiPlatformApp: App {
} }
} }
}) })
.alert(isPresented: $didCrash) {
var helpMsg = "Alert the humans by sharing what happened on the help forum."
if let errorMsg = UserDefaults.shared.object(forKey: WFDefaults.fatalErrorDescription) as? String {
helpMsg.append("\n\n\(errorMsg)")
}
return Alert(
title: Text("Crash Detected"),
message: Text(helpMsg),
primaryButton: .default(
Text("Let us know"), action: didPressCrashAlertButton
),
secondaryButton: .cancel(
Text("Dismiss"),
action: resetCrashFlags
)
)
}
.withErrorHandling() .withErrorHandling()
.environmentObject(model) .environmentObject(model)
.environment(\.managedObjectContext, LocalStorageManager.standard.container.viewContext) .environment(\.managedObjectContext, LocalStorageManager.standard.container.viewContext)
@ -165,19 +145,4 @@ struct WriteFreely_MultiPlatformApp: App {
} }
} }
} }
private func resetCrashFlags() {
UserDefaults.shared.set(false, forKey: WFDefaults.didHaveFatalError)
UserDefaults.shared.removeObject(forKey: WFDefaults.fatalErrorDescription)
}
private func didPressCrashAlertButton() {
resetCrashFlags()
#if os(macOS)
NSWorkspace().open(model.helpURL)
#else
UIApplication.shared.open(model.helpURL)
#endif
}
} }

View File

@ -7,9 +7,6 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
17027E25286741B90062EB29 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17027E24286741B80062EB29 /* Logging.swift */; };
17027E26286741B90062EB29 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17027E24286741B80062EB29 /* Logging.swift */; };
17027E27286757650062EB29 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17027E24286741B80062EB29 /* Logging.swift */; };
170A7EC126F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; }; 170A7EC126F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; };
170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; }; 170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; };
170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; }; 170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
@ -181,7 +178,6 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
17027E24286741B80062EB29 /* Logging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = "<group>"; }; 1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = "<group>"; };
170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; }; 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; };
170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = "<group>"; }; 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = "<group>"; };
@ -319,14 +315,6 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
17027E23286741910062EB29 /* Logging */ = {
isa = PBXGroup;
children = (
17027E24286741B80062EB29 /* Logging.swift */,
);
path = Logging;
sourceTree = "<group>";
};
1709ADDE251B99D40053AF79 /* Technotes */ = { 1709ADDE251B99D40053AF79 /* Technotes */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -506,7 +494,6 @@
17DF32D024C8B75C00BCE2E3 /* Account */, 17DF32D024C8B75C00BCE2E3 /* Account */,
1756AE7F24CB841200FD7257 /* Extensions */, 1756AE7F24CB841200FD7257 /* Extensions */,
17275264280997BF003D0A6A /* ErrorHandling */, 17275264280997BF003D0A6A /* ErrorHandling */,
17027E23286741910062EB29 /* Logging */,
1762DCB124EB07680019C4EB /* Models */, 1762DCB124EB07680019C4EB /* Models */,
17DF32CC24C8B72300BCE2E3 /* Navigation */, 17DF32CC24C8B72300BCE2E3 /* Navigation */,
1739B8D324EAFAB700DA7421 /* PostEditor */, 1739B8D324EAFAB700DA7421 /* PostEditor */,
@ -896,7 +883,6 @@
172E10202735C64600061372 /* WFACollection+CoreDataClass.swift in Sources */, 172E10202735C64600061372 /* WFACollection+CoreDataClass.swift in Sources */,
172E10222735C64600061372 /* WFAPost+CoreDataProperties.swift in Sources */, 172E10222735C64600061372 /* WFAPost+CoreDataProperties.swift in Sources */,
172E101D2735C5AB00061372 /* LocalStorageModel.xcdatamodeld in Sources */, 172E101D2735C5AB00061372 /* LocalStorageModel.xcdatamodeld in Sources */,
17027E27286757650062EB29 /* Logging.swift in Sources */,
17836C14273EFB870047AF61 /* UserDefaults+Extensions.swift in Sources */, 17836C14273EFB870047AF61 /* UserDefaults+Extensions.swift in Sources */,
172E10242735C72500061372 /* PreferencesModel.swift in Sources */, 172E10242735C72500061372 /* PreferencesModel.swift in Sources */,
172E10172735C2DF00061372 /* EnvironmentValues+Extensions.swift in Sources */, 172E10172735C2DF00061372 /* EnvironmentValues+Extensions.swift in Sources */,
@ -947,7 +933,6 @@
1756DC0124FEE18400207AB8 /* WFACollection+CoreDataClass.swift in Sources */, 1756DC0124FEE18400207AB8 /* WFACollection+CoreDataClass.swift in Sources */,
17DF32AA24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */, 17DF32AA24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */,
17120DA724E19D11002B9F6C /* SettingsView.swift in Sources */, 17120DA724E19D11002B9F6C /* SettingsView.swift in Sources */,
17027E25286741B90062EB29 /* Logging.swift in Sources */,
1727526628099802003D0A6A /* ErrorConstants.swift in Sources */, 1727526628099802003D0A6A /* ErrorConstants.swift in Sources */,
1756DC0324FEE18400207AB8 /* WFACollection+CoreDataProperties.swift in Sources */, 1756DC0324FEE18400207AB8 /* WFACollection+CoreDataProperties.swift in Sources */,
17120DA224E1985C002B9F6C /* AccountModel.swift in Sources */, 17120DA224E1985C002B9F6C /* AccountModel.swift in Sources */,
@ -981,7 +966,6 @@
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */, 17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
17D4926727947D780035BD7E /* MacUpdatesViewModel.swift in Sources */, 17D4926727947D780035BD7E /* MacUpdatesViewModel.swift in Sources */,
17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */, 17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */,
17027E26286741B90062EB29 /* Logging.swift in Sources */,
170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */, 170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */,
1727526B2809991A003D0A6A /* ErrorHandling.swift in Sources */, 1727526B2809991A003D0A6A /* ErrorHandling.swift in Sources */,
17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */, 17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */,