From 89b7fef45719412490d7e8d578d6b1c96d72dbcb Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 19 Mar 2021 12:34:58 -0700 Subject: [PATCH] Add and use function to log NSError objects recursively --- Autoupdate/AppInstaller.m | 16 ++++---------- Sparkle.xcodeproj/project.pbxproj | 12 ++++++++++ .../InstallerProgressAppController.m | 9 +++----- Sparkle/SPUBasicUpdateDriver.m | 8 ++----- Sparkle/SULog+NSError.h | 16 ++++++++++++++ Sparkle/SULog+NSError.m | 22 +++++++++++++++++++ 6 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 Sparkle/SULog+NSError.h create mode 100644 Sparkle/SULog+NSError.m diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index ce9308df..03839d82 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -11,6 +11,7 @@ #import "SUInstaller.h" #import "SUUpdateValidator.h" #import "SULog.h" +#import "SULog+NSError.h" #import "SUHost.h" #import "SULocalizations.h" #import "SUStandardVersionComparator.h" @@ -243,12 +244,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; - (void)unarchiverDidFailWithError:(NSError *)error { - SULog(SULogLevelError, @"Failed to unarchive file: %@", error); - - NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey]; - if (underlyingError != nil) { - SULog(SULogLevelError, @"Error: %@", underlyingError); - } + SULog(SULogLevelError, @"Failed to unarchive file"); + SULogError(error); // No longer need update validator until next possible extraction (eg: if initial delta update fails) self.updateValidator = nil; @@ -597,12 +594,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; - (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn)) { if (error != nil) { - SULog(SULogLevelError, @"Error: %@", error); - - NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey]; - if (underlyingError != nil) { - SULog(SULogLevelError, @"Error: %@", underlyingError); - } + SULogError(error); NSData *errorData = SPUArchiveRootObjectSecurely((NSError * _Nonnull)error); if (errorData != nil) { diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index ca10a691..49d097e6 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -307,6 +307,10 @@ 726F2CE81BC9C48F001971A4 /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; 726F2CE91BC9C499001971A4 /* SUOperatingSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */; }; 726F2CEB1BC9C733001971A4 /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; + 727F340B2605321D00020E85 /* SULog+NSError.m in Sources */ = {isa = PBXBuildFile; fileRef = 727F340A2605321D00020E85 /* SULog+NSError.m */; }; + 727F340C2605321D00020E85 /* SULog+NSError.m in Sources */ = {isa = PBXBuildFile; fileRef = 727F340A2605321D00020E85 /* SULog+NSError.m */; }; + 727F340D2605321D00020E85 /* SULog+NSError.m in Sources */ = {isa = PBXBuildFile; fileRef = 727F340A2605321D00020E85 /* SULog+NSError.m */; }; + 727F340E2605321D00020E85 /* SULog+NSError.m in Sources */ = {isa = PBXBuildFile; fileRef = 727F340A2605321D00020E85 /* SULog+NSError.m */; }; 728337A61C9E6FF40085AA99 /* SPUProbeInstallStatus.h in Headers */ = {isa = PBXBuildFile; fileRef = 728337A41C9E6FF40085AA99 /* SPUProbeInstallStatus.h */; }; 728337A71C9E6FF40085AA99 /* SPUProbeInstallStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 728337A51C9E6FF40085AA99 /* SPUProbeInstallStatus.m */; }; 728638ED1CAF50CE00783084 /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; @@ -1285,6 +1289,8 @@ 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUOperatingSystem.m; sourceTree = ""; }; 726FD2CB25F4BE5F00123BC6 /* fa */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fa; path = fa.lproj/MainMenu.strings; sourceTree = ""; }; 726FD2CC25F4BE5F00123BC6 /* fa */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fa; path = fa.lproj/Sparkle.strings; sourceTree = ""; }; + 727F340A2605321D00020E85 /* SULog+NSError.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SULog+NSError.m"; sourceTree = ""; }; + 727F34212605323500020E85 /* SULog+NSError.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SULog+NSError.h"; sourceTree = ""; }; 728337A41C9E6FF40085AA99 /* SPUProbeInstallStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUProbeInstallStatus.h; sourceTree = ""; }; 728337A51C9E6FF40085AA99 /* SPUProbeInstallStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUProbeInstallStatus.m; sourceTree = ""; }; 728638EE1CAF589C00783084 /* ConfigDownloader.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigDownloader.xcconfig; sourceTree = ""; }; @@ -1948,6 +1954,8 @@ 72162B071C82C9600013C1C5 /* SULocalizations.h */, 55C14F04136EF6DB00649790 /* SULog.h */, 55C14F05136EF6DB00649790 /* SULog.m */, + 727F34212605323500020E85 /* SULog+NSError.h */, + 727F340A2605321D00020E85 /* SULog+NSError.m */, 726F2CE31BC9C33D001971A4 /* SUOperatingSystem.h */, 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */, ); @@ -3384,6 +3392,7 @@ 72D954811CBACC35006F28BD /* InstallerProgressAppController.m in Sources */, 72D954831CBAD34F006F28BD /* main.m in Sources */, 72EB87EA1CB8798800C37F42 /* ShowInstallerProgress.m in Sources */, + 727F340E2605321D00020E85 /* SULog+NSError.m in Sources */, 7267E5DC1D3D8F1E00D1BF90 /* SPUMessageTypes.m in Sources */, 723C8A561E2D60DB00C14942 /* SUTouchBarButtonGroup.m in Sources */, 721652621D3C44A100FD13D8 /* SPUSystemAuthorization.m in Sources */, @@ -3478,6 +3487,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 727F340C2605321D00020E85 /* SULog+NSError.m in Sources */, 72B3DED11E23479D00457642 /* SPUDownloadedUpdate.m in Sources */, 72A5D5CB1D6929F80009E5AC /* SPUAutomaticUpdateDriver.m in Sources */, 72A5D5CE1D6929F80009E5AC /* SPUBasicUpdateDriver.m in Sources */, @@ -3554,6 +3564,7 @@ 7267E59D1D3D8A5A00D1BF90 /* SUSignatureVerifier.m in Sources */, 7267E5E71D3D90AA00D1BF90 /* SUFileManager.m in Sources */, 7267E5C21D3D8B2700D1BF90 /* SUGuidedPackageInstaller.m in Sources */, + 727F340D2605321D00020E85 /* SULog+NSError.m in Sources */, 7267E5D71D3D8D3F00D1BF90 /* SUHost.m in Sources */, 7267E5C31D3D8B2700D1BF90 /* SUInstaller.m in Sources */, 5A6DD17123FE1FFC000AEF33 /* SUSignatures.m in Sources */, @@ -3633,6 +3644,7 @@ 55C14F07136EF6DB00649790 /* SULog.m in Sources */, 726F2CE61BC9C33D001971A4 /* SUOperatingSystem.m in Sources */, 61A225A50D1C4AC000430CCD /* SUStandardVersionComparator.m in Sources */, + 727F340B2605321D00020E85 /* SULog+NSError.m in Sources */, EA1E287022B66621004AA304 /* SUSignatures.m in Sources */, 6196CFFA09C72149000DC222 /* SUStatusController.m in Sources */, 72B3DECA1E23472200457642 /* SPUDownloadedUpdate.m in Sources */, diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 75cac324..e43affa4 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -10,6 +10,7 @@ #import "InstallerProgressDelegate.h" #import "SPUMessageTypes.h" #import "SULog.h" +#import "SULog+NSError.h" #import "SUApplicationInfo.h" #import "SPUInstallerAgentProtocol.h" #import "SUInstallerAgentInitiationProtocol.h" @@ -161,12 +162,8 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; - (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn)) { if (error != nil) { - SULog(SULogLevelError, @"Agent failed with error: %@", error); - - NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey]; - if (underlyingError != nil) { - SULog(SULogLevelError, @"Error: %@", underlyingError); - } + SULog(SULogLevelError, @"Agent failed.."); + SULogError(error); [(id)self.connection.remoteObjectProxy connectionWillInvalidateWithError:error]; } diff --git a/Sparkle/SPUBasicUpdateDriver.m b/Sparkle/SPUBasicUpdateDriver.m index 9e8fd451..4f05033f 100644 --- a/Sparkle/SPUBasicUpdateDriver.m +++ b/Sparkle/SPUBasicUpdateDriver.m @@ -11,6 +11,7 @@ #import "SPUUpdaterDelegate.h" #import "SUErrors.h" #import "SULog.h" +#import "SULog+NSError.h" #import "SULocalizations.h" #import "SUHost.h" #import "SUAppcastItem.h" @@ -204,12 +205,7 @@ if (error != nil) { if (error.code != SUNoUpdateError && error.code != SUInstallationCanceledError && error.code != SUInstallationAuthorizeLaterError) { // Let's not bother logging this. - NSError *errorToDisplay = error; - int finiteRecursion=5; - do { - SULog(SULogLevelError, @"Error: %@ %@ (URL %@)", errorToDisplay.localizedDescription, errorToDisplay.localizedFailureReason, errorToDisplay.userInfo[NSURLErrorFailingURLErrorKey]); - errorToDisplay = errorToDisplay.userInfo[NSUnderlyingErrorKey]; - } while(--finiteRecursion && errorToDisplay); + SULogError(error); } // Notify host app that updater has aborted diff --git a/Sparkle/SULog+NSError.h b/Sparkle/SULog+NSError.h new file mode 100644 index 00000000..741be61a --- /dev/null +++ b/Sparkle/SULog+NSError.h @@ -0,0 +1,16 @@ +// +// SULog+NSError.h +// Sparkle +// +// Created by Mayur Pawashe on 3/19/21. +// Copyright © 2021 Sparkle Project. All rights reserved. +// + +#ifndef SULog_NSError_h +#define SULog_NSError_h + +#import + +void SULogError(NSError *error); + +#endif /* SULog_NSError_h */ diff --git a/Sparkle/SULog+NSError.m b/Sparkle/SULog+NSError.m new file mode 100644 index 00000000..f435b026 --- /dev/null +++ b/Sparkle/SULog+NSError.m @@ -0,0 +1,22 @@ +// +// SULog+NSError.m +// Sparkle +// +// Created by Mayur Pawashe on 3/19/21. +// Copyright © 2021 Sparkle Project. All rights reserved. +// + +#import "SULog+NSError.h" +#import "SULog.h" + +#include "AppKitPrevention.h" + +void SULogError(NSError *error) +{ + NSError *errorToDisplay = error; + int finiteRecursion = 5; + do { + SULog(SULogLevelError, @"Error: %@ %@ (URL %@)", errorToDisplay.localizedDescription, errorToDisplay.localizedFailureReason, errorToDisplay.userInfo[NSURLErrorFailingURLErrorKey]); + errorToDisplay = errorToDisplay.userInfo[NSUnderlyingErrorKey]; + } while(--finiteRecursion && errorToDisplay); +}