From 99652aae658f7320f70a123a25495ceca223b9a2 Mon Sep 17 00:00:00 2001 From: nythepegasus Date: Mon, 6 May 2024 17:57:13 -0400 Subject: [PATCH] Include "Enable JIT" errors in Error Log --- AltStore/Managing Apps/AppManager.swift | 86 +++++++++++---------- AltStore/My Apps/MyAppsViewController.swift | 2 + AltStoreCore/Model/LoggedError.swift | 4 + 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/AltStore/Managing Apps/AppManager.swift b/AltStore/Managing Apps/AppManager.swift index 61611d56..e1d571ae 100644 --- a/AltStore/Managing Apps/AppManager.swift +++ b/AltStore/Managing Apps/AppManager.swift @@ -317,6 +317,31 @@ extension AppManager self.run([clearAppCacheOperation], context: nil) } + + func log(_ error: Error, operation: LoggedError.Operation, app: AppProtocol) + { + // Sanitize NSError on same thread before performing background task. + let sanitizedError = (error as NSError).sanitizedForSerialization() + + DatabaseManager.shared.persistentContainer.performBackgroundTask { context in + var app = app + if let managedApp = app as? NSManagedObject, let tempApp = context.object(with: managedApp.objectID) as? AppProtocol + { + app = tempApp + } + + do + { + _ = LoggedError(error: sanitizedError, app: app, operation: operation, context: context) + try context.save() + } + catch let saveError + { + print("[ALTLog] Failed to log error \(sanitizedError.domain) code \(sanitizedError.code) for \(app.bundleIdentifier):", saveError) + } + } + } + } extension AppManager @@ -680,13 +705,20 @@ extension AppManager var installedApp: InstalledApp? } + let appName = installedApp.name let context = Context() context.installedApp = installedApp let enableJITOperation = EnableJITOperation(context: context) enableJITOperation.resultHandler = { (result) in - completionHandler(result) + switch result { + case .success: completionHandler(.success(())) + case .failure(let nsError as NSError): + let localizedTitle = String(format: NSLocalizedString("Failed to enable JIT for %@", comment: ""), appName) + let error = nsError.withLocalizedTitle(localizedTitle) + self.log(error, operation: .enableJIT, app: installedApp) + } } self.run([enableJITOperation], context: context, requiresSerialQueue: true) @@ -822,6 +854,18 @@ private extension AppManager return bundleIdentifier } + + var loggedErrorOperation: LoggedError.Operation { + switch self { + case .install: return .install + case .update: return .update + case .refresh: return .refresh + case .activate: return .activate + case .deactivate: return .deactivate + case .backup: return .backup + case .restore: return .restore + } + } } @discardableResult @@ -1722,7 +1766,7 @@ private extension AppManager let error = nsError.withLocalizedTitle(localizedTitle) group.set(.failure(error), forAppWithBundleIdentifier: operation.bundleIdentifier) - self.log(error, for: operation) + self.log(error, operation: operation.loggedErrorOperation, app: operation.app) } } @@ -1747,43 +1791,7 @@ private extension AppManager UNUserNotificationCenter.current().add(request) } - func log(_ error: Error, for operation: AppOperation) - { - // Sanitize NSError on same thread before performing background task. - let sanitizedError = (error as NSError).sanitizedForSerialization() - - let loggedErrorOperation: LoggedError.Operation = { - switch operation - { - case .install: return .install - case .update: return .update - case .refresh: return .refresh - case .activate: return .activate - case .deactivate: return .deactivate - case .backup: return .backup - case .restore: return .restore - } - }() - - DatabaseManager.shared.persistentContainer.performBackgroundTask { context in - var app = operation.app - if let managedApp = app as? NSManagedObject, let tempApp = context.object(with: managedApp.objectID) as? AppProtocol - { - app = tempApp - } - - do - { - _ = LoggedError(error: sanitizedError, app: app, operation: loggedErrorOperation, context: context) - try context.save() - } - catch let saveError - { - print("[ALTLog] Failed to log error \(sanitizedError.domain) code \(sanitizedError.code) for \(app.bundleIdentifier):", saveError) - } - } - } - + func run(_ operations: [Foundation.Operation], context: OperationContext?, requiresSerialQueue: Bool = false) { // Find "Install AltStore" operation if it already exists in `context` diff --git a/AltStore/My Apps/MyAppsViewController.swift b/AltStore/My Apps/MyAppsViewController.swift index 61a12c99..ce439c2d 100644 --- a/AltStore/My Apps/MyAppsViewController.swift +++ b/AltStore/My Apps/MyAppsViewController.swift @@ -1352,11 +1352,13 @@ private extension MyAppsViewController { if #available(iOS 17, *), !UserDefaults.standard.sidejitenable { let toastView = ToastView(error: OperationError.tooNewError) + AppManager.shared.log(OperationError.tooNewError, operation: .enableJIT, app: installedApp) toastView.show(in: self) return } if #unavailable(iOS 17), !minimuxer.ready() { let toastView = ToastView(error: MinimuxerError.NoConnection) + AppManager.shared.log(MinimuxerError.NoConnection, operation: .connection, app: installedApp) toastView.show(in: self) return } diff --git a/AltStoreCore/Model/LoggedError.swift b/AltStoreCore/Model/LoggedError.swift index 4025fa73..4932ab13 100644 --- a/AltStoreCore/Model/LoggedError.swift +++ b/AltStoreCore/Model/LoggedError.swift @@ -19,6 +19,8 @@ extension LoggedError case deactivate case backup case restore + case connection + case enableJIT } } @@ -113,6 +115,8 @@ public extension LoggedError case .deactivate: return String(format: NSLocalizedString("Deactivate %@ Failed", comment: ""), self.appName) case .backup: return String(format: NSLocalizedString("Backup %@ Failed", comment: ""), self.appName) case .restore: return String(format: NSLocalizedString("Restore %@ Failed", comment: ""), self.appName) + case .connection: return String(format: NSLocalizedString("Connection during %@ Failed", comment: ""), self.appName) + case .enableJIT: return String(format: NSLocalizedString("Enabling JIT for %@ Failed", comment: ""), self.appName) } } }