From dbcb18b1df4e35c22b8d9d99a7176ca29cd45d30 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 28 Jul 2022 07:47:39 -0400 Subject: [PATCH] Fix bad merges (#215) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Alert on error: shared code commit 00e6fabe1dad243eb49d5ffdd9695bda287b8b53 Author: Angelo Stavrow Date: Thu Jul 28 07:16:22 2022 -0400 Replace CollectionListModel with FetchRequest in CollectionListView commit d54b9471ba9da5d22c904bbb38a555529d3c15a5 Author: Angelo Stavrow Date: Thu Jul 28 07:13:13 2022 -0400 Add error handling to iOS post editor commit b48fde47147572fe78379c2bd76430f474ac4822 Author: Angelo Stavrow Date: Wed Jul 27 12:13:06 2022 -0400 Add error handling to macOS commit 695da810854414620035005b83d9c366669f96ff Author: Angelo Stavrow Date: Wed Jul 27 12:00:34 2022 -0400 Log fatal errors and present alert on next launch commit 2017a5b43768638089c24cc6ae5193d9342d528d Author: Angelo Stavrow Date: Sun Jul 24 06:22:29 2022 -0400 Clean up todo comment commit 669e07ecd9411eb1de3832514158f025655de1d7 Merge: efe173c b93e0c3 Author: Angelo Stavrow Date: Sat Jun 25 12:08:17 2022 -0400 Merge branch 'show-downloadable-logs' into log-localstore-errors commit b93e0c3547e23db53e464a57e2d8df469ae319b2 Author: Angelo Stavrow Date: Sat Jun 25 12:07:38 2022 -0400 Refactor class to use protocol commit 9b2572ba41544870ac55a6dfbbe72c808a3f54ba Author: Angelo Stavrow Date: Sat Jun 25 11:17:44 2022 -0400 Refactor logging into reuseable methods commit efe173cfca150e11f9fb3bf874f74602796277a3 Author: Angelo Stavrow Date: Fri Jun 24 08:40:10 2022 -0400 Update crash alert copy and navigate to help forum commit 5a1b40033345d216669c6f4b58ba4c28bc90b461 Author: Angelo Stavrow Date: Sat Jun 18 08:53:25 2022 -0400 Log fatal crashes and present alert on next launch commit f1b0a20643cb250b943ddc34ede6ae183ded8417 Author: Angelo Stavrow Date: Wed Jul 27 11:42:05 2022 -0400 Revert "Log fatal errors and present alert on next launch (#212)" This reverts commit 7475b577726faffec856dbdac4c69ed79501b972. commit 7475b577726faffec856dbdac4c69ed79501b972 Author: Angelo Stavrow Date: Wed Jul 27 09:47:06 2022 -0400 Log fatal errors and present alert on next launch (#212) * Log fatal crashes and present alert on next launch * Update crash alert copy and navigate to help forum * Refactor logging into reuseable methods * Refactor class to use protocol * Clean up todo comment commit a43bd801a8c8a350b488e05300fc9fb93a41af7a Author: Angelo Stavrow Date: Tue May 31 07:35:40 2022 -0400 Add error handling to Mac app commit a315b0955351e326d1244205e6d879750c1c7543 Author: Angelo Stavrow Date: Tue May 31 06:51:40 2022 -0400 Cleanup commit 7863c2ba084332a9bae6b3a3a6008e83bd11638f Author: Angelo Stavrow Date: Sat May 28 09:23:16 2022 -0400 Add error handling to post editor commit 2eba4c5c0483b8779068c52e9e6536b121e59d58 Author: Angelo Stavrow Date: Sat May 28 07:22:27 2022 -0400 Remove commented-out code commit 230f7a10762266f843b55279695a82d26d062e39 Author: Angelo Stavrow Date: Sat May 28 07:17:33 2022 -0400 Delete CollectionListModel in favour of FetchRequest in CollectionListView commit fd37a163b9ac3ec4f710e7847b81a7fb83112d3d Author: Angelo Stavrow Date: Tue May 31 07:36:43 2022 -0400 Revert "Add error handling to Mac app" This reverts commit b1a8b8b29c7a745ea9733ed0d7266c775b534b47. commit b1a8b8b29c7a745ea9733ed0d7266c775b534b47 Author: Angelo Stavrow Date: Tue May 31 07:23:41 2022 -0400 Add error handling to Mac app commit 15f84b04c026749f88404a099df3fe840e2a13a0 Author: Angelo Stavrow Date: Thu May 26 08:08:12 2022 -0400 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 commit c5b611b39e16bd339282310ab3a65fdafa98e939 Author: Angelo Stavrow Date: Thu May 26 07:31:11 2022 -0400 Add FIXME to track silent failure on fetching collections As collections are fetched and added to the `list` property in the CollectionListModel’s initializer, it’s tricky to throw an error here: we call it as a property initializer in CollectionListView, which cannot throw. Consider refactoring this logic such that we’re using, for example, a @FetchRequest in CollectionListView instead. commit b017e21e066dbcf86a3166fc06d19222047b78b9 Author: Angelo Stavrow Date: Mon May 23 15:52:20 2022 -0400 Handle purging post errors commit 11d2e41ab5d58c405160b81dcdd8597d1e2c552c Author: Angelo Stavrow Date: Mon May 23 15:12:33 2022 -0400 Add default values for some error strings commit dfb3a08608ed8ec2e2e62ad3010c9d2a67691e51 Author: Angelo Stavrow Date: Fri May 13 08:44:13 2022 -0400 Move User Defaults errors to ErrorConstants file commit 223ebf5b7cc1e34e98cc65b980673cbfdbbe527c Author: Angelo Stavrow Date: Fri May 13 08:33:32 2022 -0400 Set current error on API call handlers commit faa557c2b48e3998ace6745a0b1e035ccc320a40 Author: Angelo Stavrow Date: Fri May 13 08:01:11 2022 -0400 Set current error on API call failures commit a3b805a31907fe045f47f41da54a1169df653834 Author: Angelo Stavrow Date: Fri May 13 07:20:47 2022 -0400 Add error handling to top-level content view commit 3a53bec1845d25853bae6c05fe858fcaa195c7eb Author: Angelo Stavrow Date: Mon May 9 08:55:43 2022 -0400 Clean up WriteFreelyModel’s published vars commit aefcd0d7994393be7cafbde556cda29b8dad423d Author: Angelo Stavrow Date: Sun May 8 10:18:21 2022 -0400 Fix for temporary debugging commit bf3573895726bfc69cde3fc44960b5d81d29a473 Author: Angelo Stavrow Date: Sun May 8 09:17:05 2022 -0400 Handle errors on logout commit 01ba57ae759c87a6ac40050f39bfa9e85e46c103 Author: Angelo Stavrow Date: Sun May 8 09:16:46 2022 -0400 Move Account-related error handling up the hierarchy commit 11200a01a0cf2ad453a7a2f6d4004e129d8d2ca6 Author: Angelo Stavrow Date: Sun May 1 12:06:36 2022 -0400 Initial work on presenting alert on error * Bump writefreely-swift package minimum version commit 91e28522437f54f00b4b4854458ce5137721a481 Author: Angelo Stavrow Date: Sat May 28 06:50:34 2022 -0400 Bump writefreely-swift package minimum version --- .../Extensions/UserDefaults+Extensions.swift | 2 + Shared/LocalStorageManager.swift | 23 +++++++-- Shared/Logging/Logging.swift | 50 +++++++++++++++++++ Shared/WriteFreely_MultiPlatformApp.swift | 35 +++++++++++++ .../project.pbxproj | 16 ++++++ 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 Shared/Logging/Logging.swift diff --git a/Shared/Extensions/UserDefaults+Extensions.swift b/Shared/Extensions/UserDefaults+Extensions.swift index b010fdc..dcf0267 100644 --- a/Shared/Extensions/UserDefaults+Extensions.swift +++ b/Shared/Extensions/UserDefaults+Extensions.swift @@ -13,6 +13,8 @@ enum WFDefaults { static let automaticallyChecksForUpdates = "automaticallyChecksForUpdates" static let subscribeToBetaUpdates = "subscribeToBetaUpdates" #endif + static let didHaveFatalError = "didHaveFatalError" + static let fatalErrorDescription = "fatalErrorDescription" } extension UserDefaults { diff --git a/Shared/LocalStorageManager.swift b/Shared/LocalStorageManager.swift index ae074b4..2cf57ca 100644 --- a/Shared/LocalStorageManager.swift +++ b/Shared/LocalStorageManager.swift @@ -8,6 +8,8 @@ import AppKit final class LocalStorageManager { + private let logger = Logging(for: String(describing: LocalStorageManager.self)) + public static var standard = LocalStorageManager() public let container: NSPersistentContainer private let containerName = "LocalStorageModel" @@ -21,9 +23,11 @@ final class LocalStorageManager { func saveContext() { if container.viewContext.hasChanges { do { + logger.log("Saving context to local store started...") try container.viewContext.save() + logger.log("Context saved to local store.") } catch { - fatalError(LocalStoreError.couldNotSaveContext.localizedDescription) + logger.logCrashAndSetFlag(error: LocalStoreError.couldNotSaveContext) } } } @@ -33,8 +37,11 @@ final class LocalStorageManager { let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) do { + logger.log("Purging user collections from local store...") try container.viewContext.executeAndMergeChanges(using: deleteRequest) + logger.log("User collections purged from local store.") } catch { + logger.log("\(LocalStoreError.couldNotPurgeCollections.localizedDescription)", level: .error) throw LocalStoreError.couldNotPurgeCollections } } @@ -60,9 +67,11 @@ private extension LocalStorageManager { } container.loadPersistentStores { _, error in + self.logger.log("Loading local store...") if let error = error { - fatalError(LocalStoreError.couldNotLoadStore(error.localizedDescription).localizedDescription) + self.logger.logCrashAndSetFlag(error: LocalStoreError.couldNotLoadStore(error.localizedDescription)) } + self.logger.log("Loaded local store.") } migrateStore(for: container) container.viewContext.automaticallyMergesChangesFromParent = true @@ -83,20 +92,24 @@ private extension LocalStorageManager { // Attempt to migrate the old store over to the shared store URL. do { + self.logger.log("Migrating local store to shared store...") try coordinator.migratePersistentStore(oldStore, to: sharedStoreURL, options: nil, withType: NSSQLiteStoreType) + self.logger.log("Migrated local store to shared store.") } catch { - fatalError(LocalStoreError.couldNotMigrateStore(error.localizedDescription).localizedDescription) + logger.logCrashAndSetFlag(error: LocalStoreError.couldNotMigrateStore(error.localizedDescription)) } // Attempt to delete the old store. do { + logger.log("Deleting migrated local store...") try FileManager.default.removeItem(at: oldStoreURL) + logger.log("Deleted migrated local store.") } catch { - fatalError( - LocalStoreError.couldNotDeleteStoreAfterMigration(error.localizedDescription).localizedDescription + logger.logCrashAndSetFlag( + error: LocalStoreError.couldNotDeleteStoreAfterMigration(error.localizedDescription) ) } } diff --git a/Shared/Logging/Logging.swift b/Shared/Logging/Logging.swift new file mode 100644 index 0000000..64d7f9c --- /dev/null +++ b/Shared/Logging/Logging.swift @@ -0,0 +1,50 @@ +// +// 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) + } + +} diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index d17c965..51e261a 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -30,6 +30,8 @@ struct WriteFreely_MultiPlatformApp: App { @State private var selectedTab = 0 #endif + @State private var didCrash = UserDefaults.shared.bool(forKey: WFDefaults.didHaveFatalError) + var body: some Scene { WindowGroup { ContentView() @@ -48,6 +50,24 @@ 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() .environmentObject(model) .environment(\.managedObjectContext, LocalStorageManager.standard.container.viewContext) @@ -145,4 +165,19 @@ 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 + } + } diff --git a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj index 812ebdb..0a94e72 100644 --- a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj +++ b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* 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 */; }; 170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; }; 170DFA35251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; }; 17120DA124E19839002B9F6C /* AccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17A5388D24DDEC7400DEFF9A /* AccountView.swift */; }; @@ -176,6 +179,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 17027E24286741B80062EB29 /* Logging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = ""; }; 1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = ""; }; 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = ""; }; 17120DA424E19CBF002B9F6C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = ""; }; @@ -312,6 +316,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 17027E23286741910062EB29 /* Logging */ = { + isa = PBXGroup; + children = ( + 17027E24286741B80062EB29 /* Logging.swift */, + ); + path = Logging; + sourceTree = ""; + }; 1709ADDE251B99D40053AF79 /* Technotes */ = { isa = PBXGroup; children = ( @@ -491,6 +503,7 @@ 17DF32D024C8B75C00BCE2E3 /* Account */, 1756AE7F24CB841200FD7257 /* Extensions */, 17275264280997BF003D0A6A /* ErrorHandling */, + 17027E23286741910062EB29 /* Logging */, 1762DCB124EB07680019C4EB /* Models */, 17DF32CC24C8B72300BCE2E3 /* Navigation */, 1739B8D324EAFAB700DA7421 /* PostEditor */, @@ -879,6 +892,7 @@ 172E10202735C64600061372 /* WFACollection+CoreDataClass.swift in Sources */, 172E10222735C64600061372 /* WFAPost+CoreDataProperties.swift in Sources */, 172E101D2735C5AB00061372 /* LocalStorageModel.xcdatamodeld in Sources */, + 17027E27286757650062EB29 /* Logging.swift in Sources */, 17836C14273EFB870047AF61 /* UserDefaults+Extensions.swift in Sources */, 172E10242735C72500061372 /* PreferencesModel.swift in Sources */, 172E10172735C2DF00061372 /* EnvironmentValues+Extensions.swift in Sources */, @@ -928,6 +942,7 @@ 1756DC0124FEE18400207AB8 /* WFACollection+CoreDataClass.swift in Sources */, 17DF32AA24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */, 17120DA724E19D11002B9F6C /* SettingsView.swift in Sources */, + 17027E25286741B90062EB29 /* Logging.swift in Sources */, 1727526628099802003D0A6A /* ErrorConstants.swift in Sources */, 1756DC0324FEE18400207AB8 /* WFACollection+CoreDataProperties.swift in Sources */, 17120DA224E1985C002B9F6C /* AccountModel.swift in Sources */, @@ -961,6 +976,7 @@ 17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */, 17D4926727947D780035BD7E /* MacUpdatesViewModel.swift in Sources */, 17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */, + 17027E26286741B90062EB29 /* Logging.swift in Sources */, 1727526B2809991A003D0A6A /* ErrorHandling.swift in Sources */, 17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */, 17C42E71250AAFD500072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */,