Skip to content

Commit dbcb18b

Browse files
Fix bad merges (#215)
* Alert on error: shared code commit 00e6fabe1dad243eb49d5ffdd9695bda287b8b53 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Thu Jul 28 07:16:22 2022 -0400 Replace CollectionListModel with FetchRequest in CollectionListView commit d54b9471ba9da5d22c904bbb38a555529d3c15a5 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Thu Jul 28 07:13:13 2022 -0400 Add error handling to iOS post editor commit b48fde47147572fe78379c2bd76430f474ac4822 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Wed Jul 27 12:13:06 2022 -0400 Add error handling to macOS commit 695da810854414620035005b83d9c366669f96ff Author: Angelo Stavrow <contact@angelostavrow.com> Date: Wed Jul 27 12:00:34 2022 -0400 Log fatal errors and present alert on next launch commit 2017a5b Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sun Jul 24 06:22:29 2022 -0400 Clean up todo comment commit 669e07e Merge: efe173c b93e0c3 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat Jun 25 12:08:17 2022 -0400 Merge branch 'show-downloadable-logs' into log-localstore-errors commit b93e0c3 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat Jun 25 12:07:38 2022 -0400 Refactor class to use protocol commit 9b2572b Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat Jun 25 11:17:44 2022 -0400 Refactor logging into reuseable methods commit efe173c Author: Angelo Stavrow <contact@angelostavrow.com> Date: Fri Jun 24 08:40:10 2022 -0400 Update crash alert copy and navigate to help forum commit 5a1b400 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat Jun 18 08:53:25 2022 -0400 Log fatal crashes and present alert on next launch commit f1b0a20 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Wed Jul 27 11:42:05 2022 -0400 Revert "Log fatal errors and present alert on next launch (#212)" This reverts commit 7475b57. commit 7475b57 Author: Angelo Stavrow <contact@angelostavrow.com> 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 a43bd80 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Tue May 31 07:35:40 2022 -0400 Add error handling to Mac app commit a315b09 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Tue May 31 06:51:40 2022 -0400 Cleanup commit 7863c2b Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat May 28 09:23:16 2022 -0400 Add error handling to post editor commit 2eba4c5 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat May 28 07:22:27 2022 -0400 Remove commented-out code commit 230f7a1 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat May 28 07:17:33 2022 -0400 Delete CollectionListModel in favour of FetchRequest in CollectionListView commit fd37a16 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Tue May 31 07:36:43 2022 -0400 Revert "Add error handling to Mac app" This reverts commit b1a8b8b. commit b1a8b8b Author: Angelo Stavrow <contact@angelostavrow.com> Date: Tue May 31 07:23:41 2022 -0400 Add error handling to Mac app commit 15f84b0 Author: Angelo Stavrow <contact@angelostavrow.com> 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 c5b611b Author: Angelo Stavrow <contact@angelostavrow.com> 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 b017e21 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Mon May 23 15:52:20 2022 -0400 Handle purging post errors commit 11d2e41 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Mon May 23 15:12:33 2022 -0400 Add default values for some error strings commit dfb3a08 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Fri May 13 08:44:13 2022 -0400 Move User Defaults errors to ErrorConstants file commit 223ebf5 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Fri May 13 08:33:32 2022 -0400 Set current error on API call handlers commit faa557c Author: Angelo Stavrow <contact@angelostavrow.com> Date: Fri May 13 08:01:11 2022 -0400 Set current error on API call failures commit a3b805a Author: Angelo Stavrow <contact@angelostavrow.com> Date: Fri May 13 07:20:47 2022 -0400 Add error handling to top-level content view commit 3a53bec Author: Angelo Stavrow <contact@angelostavrow.com> Date: Mon May 9 08:55:43 2022 -0400 Clean up WriteFreelyModel’s published vars commit aefcd0d Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sun May 8 10:18:21 2022 -0400 Fix for temporary debugging commit bf35738 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sun May 8 09:17:05 2022 -0400 Handle errors on logout commit 01ba57a Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sun May 8 09:16:46 2022 -0400 Move Account-related error handling up the hierarchy commit 11200a0 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sun May 1 12:06:36 2022 -0400 Initial work on presenting alert on error * Bump writefreely-swift package minimum version commit 91e2852 Author: Angelo Stavrow <contact@angelostavrow.com> Date: Sat May 28 06:50:34 2022 -0400 Bump writefreely-swift package minimum version
1 parent 93c0162 commit dbcb18b

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed

Shared/Extensions/UserDefaults+Extensions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ enum WFDefaults {
1313
static let automaticallyChecksForUpdates = "automaticallyChecksForUpdates"
1414
static let subscribeToBetaUpdates = "subscribeToBetaUpdates"
1515
#endif
16+
static let didHaveFatalError = "didHaveFatalError"
17+
static let fatalErrorDescription = "fatalErrorDescription"
1618
}
1719

1820
extension UserDefaults {

Shared/LocalStorageManager.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import AppKit
88

99
final class LocalStorageManager {
1010

11+
private let logger = Logging(for: String(describing: LocalStorageManager.self))
12+
1113
public static var standard = LocalStorageManager()
1214
public let container: NSPersistentContainer
1315
private let containerName = "LocalStorageModel"
@@ -21,9 +23,11 @@ final class LocalStorageManager {
2123
func saveContext() {
2224
if container.viewContext.hasChanges {
2325
do {
26+
logger.log("Saving context to local store started...")
2427
try container.viewContext.save()
28+
logger.log("Context saved to local store.")
2529
} catch {
26-
fatalError(LocalStoreError.couldNotSaveContext.localizedDescription)
30+
logger.logCrashAndSetFlag(error: LocalStoreError.couldNotSaveContext)
2731
}
2832
}
2933
}
@@ -33,8 +37,11 @@ final class LocalStorageManager {
3337
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
3438

3539
do {
40+
logger.log("Purging user collections from local store...")
3641
try container.viewContext.executeAndMergeChanges(using: deleteRequest)
42+
logger.log("User collections purged from local store.")
3743
} catch {
44+
logger.log("\(LocalStoreError.couldNotPurgeCollections.localizedDescription)", level: .error)
3845
throw LocalStoreError.couldNotPurgeCollections
3946
}
4047
}
@@ -60,9 +67,11 @@ private extension LocalStorageManager {
6067
}
6168

6269
container.loadPersistentStores { _, error in
70+
self.logger.log("Loading local store...")
6371
if let error = error {
64-
fatalError(LocalStoreError.couldNotLoadStore(error.localizedDescription).localizedDescription)
72+
self.logger.logCrashAndSetFlag(error: LocalStoreError.couldNotLoadStore(error.localizedDescription))
6573
}
74+
self.logger.log("Loaded local store.")
6675
}
6776
migrateStore(for: container)
6877
container.viewContext.automaticallyMergesChangesFromParent = true
@@ -83,20 +92,24 @@ private extension LocalStorageManager {
8392

8493
// Attempt to migrate the old store over to the shared store URL.
8594
do {
95+
self.logger.log("Migrating local store to shared store...")
8696
try coordinator.migratePersistentStore(oldStore,
8797
to: sharedStoreURL,
8898
options: nil,
8999
withType: NSSQLiteStoreType)
100+
self.logger.log("Migrated local store to shared store.")
90101
} catch {
91-
fatalError(LocalStoreError.couldNotMigrateStore(error.localizedDescription).localizedDescription)
102+
logger.logCrashAndSetFlag(error: LocalStoreError.couldNotMigrateStore(error.localizedDescription))
92103
}
93104

94105
// Attempt to delete the old store.
95106
do {
107+
logger.log("Deleting migrated local store...")
96108
try FileManager.default.removeItem(at: oldStoreURL)
109+
logger.log("Deleted migrated local store.")
97110
} catch {
98-
fatalError(
99-
LocalStoreError.couldNotDeleteStoreAfterMigration(error.localizedDescription).localizedDescription
111+
logger.logCrashAndSetFlag(
112+
error: LocalStoreError.couldNotDeleteStoreAfterMigration(error.localizedDescription)
100113
)
101114
}
102115
}

Shared/Logging/Logging.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Logging.swift
3+
// WriteFreely-MultiPlatform
4+
//
5+
// Created by Angelo Stavrow on 2022-06-25.
6+
//
7+
8+
import Foundation
9+
import os
10+
import OSLog
11+
12+
protocol LogWriter {
13+
func log(_ message: String, withSensitiveInfo privateInfo: String?, level: OSLogType)
14+
func logCrashAndSetFlag(error: Error)
15+
}
16+
17+
final class Logging {
18+
19+
private let logger: Logger
20+
private let subsystem = Bundle.main.bundleIdentifier!
21+
22+
init(for category: String = "") {
23+
self.logger = Logger(subsystem: subsystem, category: category)
24+
}
25+
26+
}
27+
28+
extension Logging: LogWriter {
29+
30+
func log(
31+
_ message: String,
32+
withSensitiveInfo privateInfo: String? = nil,
33+
level: OSLogType = .default
34+
) {
35+
if let privateInfo = privateInfo {
36+
logger.log(level: level, "\(message): \(privateInfo, privacy: .sensitive)")
37+
} else {
38+
logger.log(level: level, "\(message)")
39+
}
40+
}
41+
42+
func logCrashAndSetFlag(error: Error) {
43+
let errorDescription = error.localizedDescription
44+
UserDefaults.shared.set(true, forKey: WFDefaults.didHaveFatalError)
45+
UserDefaults.shared.set(errorDescription, forKey: WFDefaults.fatalErrorDescription)
46+
logger.log(level: .error, "\(errorDescription)")
47+
fatalError(errorDescription)
48+
}
49+
50+
}

Shared/WriteFreely_MultiPlatformApp.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct WriteFreely_MultiPlatformApp: App {
3030
@State private var selectedTab = 0
3131
#endif
3232

33+
@State private var didCrash = UserDefaults.shared.bool(forKey: WFDefaults.didHaveFatalError)
34+
3335
var body: some Scene {
3436
WindowGroup {
3537
ContentView()
@@ -48,6 +50,24 @@ struct WriteFreely_MultiPlatformApp: App {
4850
}
4951
}
5052
})
53+
.alert(isPresented: $didCrash) {
54+
var helpMsg = "Alert the humans by sharing what happened on the help forum."
55+
if let errorMsg = UserDefaults.shared.object(forKey: WFDefaults.fatalErrorDescription) as? String {
56+
helpMsg.append("\n\n\(errorMsg)")
57+
}
58+
59+
return Alert(
60+
title: Text("Crash Detected"),
61+
message: Text(helpMsg),
62+
primaryButton: .default(
63+
Text("Let us know"), action: didPressCrashAlertButton
64+
),
65+
secondaryButton: .cancel(
66+
Text("Dismiss"),
67+
action: resetCrashFlags
68+
)
69+
)
70+
}
5171
.withErrorHandling()
5272
.environmentObject(model)
5373
.environment(\.managedObjectContext, LocalStorageManager.standard.container.viewContext)
@@ -145,4 +165,19 @@ struct WriteFreely_MultiPlatformApp: App {
145165
}
146166
}
147167
}
168+
169+
private func resetCrashFlags() {
170+
UserDefaults.shared.set(false, forKey: WFDefaults.didHaveFatalError)
171+
UserDefaults.shared.removeObject(forKey: WFDefaults.fatalErrorDescription)
172+
}
173+
174+
private func didPressCrashAlertButton() {
175+
resetCrashFlags()
176+
#if os(macOS)
177+
NSWorkspace().open(model.helpURL)
178+
#else
179+
UIApplication.shared.open(model.helpURL)
180+
#endif
181+
}
182+
148183
}

WriteFreely-MultiPlatform.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
17027E25286741B90062EB29 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17027E24286741B80062EB29 /* Logging.swift */; };
11+
17027E26286741B90062EB29 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17027E24286741B80062EB29 /* Logging.swift */; };
12+
17027E27286757650062EB29 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17027E24286741B80062EB29 /* Logging.swift */; };
1013
170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
1114
170DFA35251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
1215
17120DA124E19839002B9F6C /* AccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17A5388D24DDEC7400DEFF9A /* AccountView.swift */; };
@@ -176,6 +179,7 @@
176179
/* End PBXCopyFilesBuildPhase section */
177180

178181
/* Begin PBXFileReference section */
182+
17027E24286741B80062EB29 /* Logging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
179183
1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = "<group>"; };
180184
170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = "<group>"; };
181185
17120DA424E19CBF002B9F6C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
@@ -312,6 +316,14 @@
312316
/* End PBXFrameworksBuildPhase section */
313317

314318
/* Begin PBXGroup section */
319+
17027E23286741910062EB29 /* Logging */ = {
320+
isa = PBXGroup;
321+
children = (
322+
17027E24286741B80062EB29 /* Logging.swift */,
323+
);
324+
path = Logging;
325+
sourceTree = "<group>";
326+
};
315327
1709ADDE251B99D40053AF79 /* Technotes */ = {
316328
isa = PBXGroup;
317329
children = (
@@ -491,6 +503,7 @@
491503
17DF32D024C8B75C00BCE2E3 /* Account */,
492504
1756AE7F24CB841200FD7257 /* Extensions */,
493505
17275264280997BF003D0A6A /* ErrorHandling */,
506+
17027E23286741910062EB29 /* Logging */,
494507
1762DCB124EB07680019C4EB /* Models */,
495508
17DF32CC24C8B72300BCE2E3 /* Navigation */,
496509
1739B8D324EAFAB700DA7421 /* PostEditor */,
@@ -879,6 +892,7 @@
879892
172E10202735C64600061372 /* WFACollection+CoreDataClass.swift in Sources */,
880893
172E10222735C64600061372 /* WFAPost+CoreDataProperties.swift in Sources */,
881894
172E101D2735C5AB00061372 /* LocalStorageModel.xcdatamodeld in Sources */,
895+
17027E27286757650062EB29 /* Logging.swift in Sources */,
882896
17836C14273EFB870047AF61 /* UserDefaults+Extensions.swift in Sources */,
883897
172E10242735C72500061372 /* PreferencesModel.swift in Sources */,
884898
172E10172735C2DF00061372 /* EnvironmentValues+Extensions.swift in Sources */,
@@ -928,6 +942,7 @@
928942
1756DC0124FEE18400207AB8 /* WFACollection+CoreDataClass.swift in Sources */,
929943
17DF32AA24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */,
930944
17120DA724E19D11002B9F6C /* SettingsView.swift in Sources */,
945+
17027E25286741B90062EB29 /* Logging.swift in Sources */,
931946
1727526628099802003D0A6A /* ErrorConstants.swift in Sources */,
932947
1756DC0324FEE18400207AB8 /* WFACollection+CoreDataProperties.swift in Sources */,
933948
17120DA224E1985C002B9F6C /* AccountModel.swift in Sources */,
@@ -961,6 +976,7 @@
961976
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
962977
17D4926727947D780035BD7E /* MacUpdatesViewModel.swift in Sources */,
963978
17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */,
979+
17027E26286741B90062EB29 /* Logging.swift in Sources */,
964980
1727526B2809991A003D0A6A /* ErrorHandling.swift in Sources */,
965981
17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */,
966982
17C42E71250AAFD500072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */,

0 commit comments

Comments
 (0)