From 3587b5eba0fcf09f09bf65832876550537d1be66 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 12 Mar 2026 20:18:54 -0700 Subject: [PATCH] Add operation column that says what the app was actually doing. --- Mac/AppDelegate.swift | 28 +++++++++++-------- .../Sources/ErrorLog/ErrorLogDatabase.swift | 9 +++--- .../Sources/ErrorLog/ErrorLogEntry.swift | 6 +++- .../ErrorLog/ErrorLogNotification.swift | 4 ++- .../Sources/ErrorLog/ErrorLogTable.swift | 6 ++-- .../ErrorLogTests/ErrorLogDatabaseTests.swift | 11 ++++---- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 61aee0b7b..f5857999d 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -265,6 +265,10 @@ let appName = "NetNewsWire" CrashReporter.check(crashReporter: crashReporter) } } + + #if DEBUG + postFakeErrorsForTesting() + #endif } func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool { @@ -701,20 +705,20 @@ extension AppDelegate { #if DEBUG func postFakeErrorsForTesting() { - let fakeErrors: [(String, Int, String)] = [ - ("On My Mac", AccountType.onMyMac.rawValue, "HTTP 404 Not Found: https://example.com/feed.xml"), - ("Feedbin", AccountType.feedbin.rawValue, "HTTP 401 Unauthorized"), - ("iCloud", AccountType.cloudKit.rawValue, "HTTP 429 Too Many Requests: https://daringfireball.net/feeds/main"), - ("NewsBlur", AccountType.newsBlur.rawValue, "The operation couldn\u{2019}t be completed. Network connection lost."), - ("FreshRSS", AccountType.freshRSS.rawValue, "HTTP 403 Forbidden: https://freshrss.example.com/api/greader.php"), - ("Feedly", AccountType.feedly.rawValue, "HTTP 500 Internal Server Error: https://cloud.feedly.com/v3/streams"), - ("Inoreader", AccountType.inoreader.rawValue, "The request timed out."), - ("BazQux", AccountType.bazQux.rawValue, "HTTP 502 Bad Gateway: https://www.bazqux.com/reader/api"), - ("The Old Reader", AccountType.theOldReader.rawValue, "A server with the specified hostname could not be found.") + let fakeErrors: [(String, Int, String, String)] = [ + ("On My Mac", AccountType.onMyMac.rawValue, "Downloading feed", "HTTP 404 Not Found: https://example.com/feed.xml"), + ("Feedbin", AccountType.feedbin.rawValue, "Syncing starred status", "HTTP 401 Unauthorized"), + ("iCloud", AccountType.cloudKit.rawValue, "Refreshing", "HTTP 429 Too Many Requests: https://daringfireball.net/feeds/main"), + ("NewsBlur", AccountType.newsBlur.rawValue, "Syncing read status", "The operation couldn\u{2019}t be completed. Network connection lost."), + ("FreshRSS", AccountType.freshRSS.rawValue, "Refreshing articles", "HTTP 403 Forbidden: https://freshrss.example.com/api/greader.php"), + ("Feedly", AccountType.feedly.rawValue, "Restoring feed", "HTTP 500 Internal Server Error: https://cloud.feedly.com/v3/streams"), + ("Inoreader", AccountType.inoreader.rawValue, "Refreshing", "The request timed out."), + ("BazQux", AccountType.bazQux.rawValue, "Removing feed", "HTTP 502 Bad Gateway: https://www.bazqux.com/reader/api"), + ("The Old Reader", AccountType.theOldReader.rawValue, "Refreshing articles", "A server with the specified hostname could not be found.") ] - for (accountName, accountType, message) in fakeErrors { - let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: accountName, sourceID: accountType, errorMessage: message) + for (accountName, accountType, operation, message) in fakeErrors { + let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: accountName, sourceID: accountType, operation: operation, errorMessage: message) NotificationCenter.default.post(name: .appDidEncounterError, object: self, userInfo: errorLogUserInfo) } } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift index 070f52da4..51114d5db 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift @@ -14,7 +14,7 @@ public actor ErrorLogDatabase { private let database: FMDatabase - private static let tableCreationStatements = "CREATE TABLE if not EXISTS errors (id INTEGER PRIMARY KEY AUTOINCREMENT, date REAL NOT NULL, sourceName TEXT NOT NULL, sourceID INTEGER NOT NULL, errorMessage TEXT NOT NULL);" + private static let tableCreationStatements = "CREATE TABLE if not EXISTS errors (id INTEGER PRIMARY KEY AUTOINCREMENT, date REAL NOT NULL, sourceName TEXT NOT NULL, sourceID INTEGER NOT NULL, operation TEXT NOT NULL, errorMessage TEXT NOT NULL);" private static let pruneLimit = 200 @@ -31,8 +31,8 @@ public actor ErrorLogDatabase { } } - public func addEntry(sourceName: String, sourceID: Int, errorMessage: String) { - ErrorLogTable.insertEntry(sourceName: sourceName, sourceID: sourceID, errorMessage: errorMessage, database: database) + public func addEntry(sourceName: String, sourceID: Int, operation: String, errorMessage: String) { + ErrorLogTable.insertEntry(sourceName: sourceName, sourceID: sourceID, operation: operation, errorMessage: errorMessage, database: database) } public func allEntries() -> [ErrorLogEntry] { @@ -47,8 +47,9 @@ public actor ErrorLogDatabase { let sourceID = notification.userInfo?[ErrorLogUserInfoKey.sourceID] as? Int else { return } + let operation = notification.userInfo?[ErrorLogUserInfoKey.operation] as? String ?? "" Task { - await addEntry(sourceName: sourceName, sourceID: sourceID, errorMessage: errorMessage) + await addEntry(sourceName: sourceName, sourceID: sourceID, operation: operation, errorMessage: errorMessage) } } } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift index 4bafbf0c7..d7f820f3f 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift @@ -14,13 +14,15 @@ public struct ErrorLogEntry: Sendable { public let date: Date public let sourceName: String public let sourceID: Int // 0-99 reserved for AccountType.rawValue. 100 and up for other components. + public let operation: String public let errorMessage: String - public init(id: Int, date: Date, sourceName: String, sourceID: Int, errorMessage: String) { + public init(id: Int, date: Date, sourceName: String, sourceID: Int, operation: String, errorMessage: String) { self.id = id self.date = date self.sourceName = sourceName self.sourceID = sourceID + self.operation = operation self.errorMessage = errorMessage } @@ -29,6 +31,7 @@ public struct ErrorLogEntry: Sendable { static let date = "date" static let sourceName = "sourceName" static let sourceID = "sourceID" + static let operation = "operation" static let errorMessage = "errorMessage" } @@ -37,6 +40,7 @@ public struct ErrorLogEntry: Sendable { DatabaseKey.date: date.timeIntervalSince1970, DatabaseKey.sourceName: sourceName, DatabaseKey.sourceID: sourceID, + DatabaseKey.operation: operation, DatabaseKey.errorMessage: errorMessage ] } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift index 2a4f5085f..1ac261aa6 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift @@ -18,12 +18,14 @@ public struct ErrorLogUserInfoKey { public static let sourceName = "sourceName" public static let sourceID = "sourceID" // 0-99 are AccountType raw values. 100 and greater are for other components. + public static let operation = "operation" public static let errorMessage = "errorMessage" - public static func userInfo(sourceName: String, sourceID: Int, errorMessage: String) -> [String: Any] { + public static func userInfo(sourceName: String, sourceID: Int, operation: String, errorMessage: String) -> [String: Any] { [ Self.sourceName: sourceName, Self.sourceID: sourceID, + Self.operation: operation, Self.errorMessage: errorMessage ] } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift index ca8325c10..98c27dc10 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift @@ -13,11 +13,12 @@ struct ErrorLogTable { static let name = "errors" - static func insertEntry(sourceName: String, sourceID: Int, errorMessage: String, database: FMDatabase) { + static func insertEntry(sourceName: String, sourceID: Int, operation: String, errorMessage: String, database: FMDatabase) { let dictionary: DatabaseDictionary = [ ErrorLogEntry.DatabaseKey.date: Date().timeIntervalSince1970, ErrorLogEntry.DatabaseKey.sourceName: sourceName, ErrorLogEntry.DatabaseKey.sourceID: sourceID, + ErrorLogEntry.DatabaseKey.operation: operation, ErrorLogEntry.DatabaseKey.errorMessage: errorMessage ] database.insertRow(dictionary, insertType: .normal, tableName: name) @@ -55,7 +56,8 @@ private extension ErrorLogTable { let id = Int(row.longLongInt(forColumn: ErrorLogEntry.DatabaseKey.id)) let date = Date(timeIntervalSince1970: row.double(forColumn: ErrorLogEntry.DatabaseKey.date)) let sourceID = Int(row.int(forColumn: ErrorLogEntry.DatabaseKey.sourceID)) + let operation = row.string(forColumn: ErrorLogEntry.DatabaseKey.operation) ?? "" - return ErrorLogEntry(id: id, date: date, sourceName: sourceName, sourceID: sourceID, errorMessage: errorMessage) + return ErrorLogEntry(id: id, date: date, sourceName: sourceName, sourceID: sourceID, operation: operation, errorMessage: errorMessage) } } diff --git a/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift b/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift index 59321b4b9..ca23ce957 100644 --- a/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift +++ b/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift @@ -28,7 +28,7 @@ import Foundation defer { deleteDatabaseFiles(at: path) } let database = ErrorLogDatabase(databasePath: path) - await database.addEntry(sourceName: "TestAccount", sourceID: 1, errorMessage: "Something went wrong") + await database.addEntry(sourceName: "TestAccount", sourceID: 1, operation: "Refreshing", errorMessage: "Something went wrong") let entries = await database.allEntries() #expect(entries.count == 1) @@ -36,6 +36,7 @@ import Foundation let entry = entries[0] #expect(entry.sourceName == "TestAccount") #expect(entry.sourceID == 1) + #expect(entry.operation == "Refreshing") #expect(entry.errorMessage == "Something went wrong") #expect(entry.id > 0) } @@ -45,9 +46,9 @@ import Foundation defer { deleteDatabaseFiles(at: path) } let database = ErrorLogDatabase(databasePath: path) - await database.addEntry(sourceName: "First", sourceID: 1, errorMessage: "Error 1") - await database.addEntry(sourceName: "Second", sourceID: 2, errorMessage: "Error 2") - await database.addEntry(sourceName: "Third", sourceID: 3, errorMessage: "Error 3") + await database.addEntry(sourceName: "First", sourceID: 1, operation: "Refreshing", errorMessage: "Error 1") + await database.addEntry(sourceName: "Second", sourceID: 2, operation: "Syncing", errorMessage: "Error 2") + await database.addEntry(sourceName: "Third", sourceID: 3, operation: "Downloading feed", errorMessage: "Error 3") let entries = await database.allEntries() #expect(entries.count == 3) @@ -64,7 +65,7 @@ import Foundation let database = ErrorLogDatabase(databasePath: path) for i in 1...210 { - await database.addEntry(sourceName: "Account", sourceID: 1, errorMessage: "Error \(i)") + await database.addEntry(sourceName: "Account", sourceID: 1, operation: "Refreshing", errorMessage: "Error \(i)") } let entriesBeforePrune = await database.allEntries()