Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Change exceptions thrown in setup tasks into task failures, and add more details to error logging #1071

Merged
merged 5 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void RemoveAccount(string loginId)
}
catch (Exception ex)
{
GlobalLog.Logger?.ReportError($"RemoveAccount() failed - developerId: {loginId} Error: {ex}");
GlobalLog.Logger?.ReportError($"RemoveAccount() failed - developerId: {loginId}.", ex);
throw;
}
}
Expand Down
4 changes: 2 additions & 2 deletions settings/DevHome.Settings/Views/AccountsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task ShowLoginUIAsync(string loginEntryPoint, Page parentPage, Acco
}
catch (Exception ex)
{
GlobalLog.Logger?.ReportError($"ShowLoginUIAsync(): loginUIContentDialog failed - Error: {ex}");
GlobalLog.Logger?.ReportError($"ShowLoginUIAsync(): loginUIContentDialog failed.", ex);
}

accountProvider.RefreshLoggedInAccounts();
Expand All @@ -145,7 +145,7 @@ private async Task ConfigureLoginUIRenderer(AdaptiveCardRenderer renderer)
}
catch (Exception ex)
{
GlobalLog.Logger?.ReportError($"Failure occurred while retrieving the HostConfig file - Error: {ex} HostConfigFileName: {hostConfigFileName}");
GlobalLog.Logger?.ReportError($"Failure occurred while retrieving the HostConfig file - HostConfigFileName: {hostConfigFileName}.", ex);
}

// Add host config for current theme to renderer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public int FormatPartitionAsDevDrive(char curDriveLetter, string driveLabel)
}
catch (ManagementException e)
{
Log.Logger?.ReportError(Log.Component.DevDrive, nameof(FormatPartitionAsDevDrive), $"A management exception occurred while formating Dev Drive Error: 0x{e.HResult:X}, error msg: {e.Message}");
Log.Logger?.ReportError(Log.Component.DevDrive, nameof(FormatPartitionAsDevDrive), $"A management exception occurred while formating Dev Drive Error.", e);
return e.HResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public static (RemoteObject<T>, Process) CreateOutOfProcessObjectAndGetProcess<T
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.IPCClient, $"Error occuring while setting up elevated process: {e.Message}");
Log.Logger?.ReportError(Log.Component.IPCClient, $"Error occuring while setting up elevated process.", e);

// Release the "mutex" if there is any error.
// On success, the mutex will be released after work is done.
Expand Down Expand Up @@ -375,7 +375,7 @@ public static void CompleteRemoteObjectInitialization<T>(
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.IPCServer, $"Error occurred during setup: {e.Message}");
Log.Logger?.ReportError(Log.Component.IPCServer, $"Error occurred during setup.", e);
mappedMemory.HResult = e.HResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IAsyncOperation<ElevatedConfigureTaskResult> ApplyConfiguration(StorageFi
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.Configuration, $"Failed to apply configuration: {e.Message}");
Log.Logger?.ReportError(Log.Component.Configuration, $"Failed to apply configuration.", e);
taskResult.TaskSucceeded = false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation and Contributors
// Licensed under the MIT license.

using DevHome.Logging;
using DevHome.SetupFlow.Common.Extensions;
using DevHome.SetupFlow.Common.Helpers;
using DevHome.SetupFlow.Common.WindowsPackageManager;
Expand Down Expand Up @@ -38,53 +39,63 @@ public IAsyncOperation<ElevatedInstallTaskResult> InstallPackage(string packageI
{
return Task.Run(async () =>
{
Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Elevated install requested for package [{packageId}] from catalog [{catalogName}]");
var result = new ElevatedInstallTaskResult();

var packageManager = _wingetFactory.CreatePackageManager();

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Connecting to catalog [{catalogName}]");
var catalogReference = packageManager.GetPackageCatalogByName(catalogName);
var connectResult = await catalogReference.ConnectAsync();
if (connectResult.Status != ConnectResultStatus.Ok)
try
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to connect to the catalog [{catalogName}] with status {connectResult.Status}");
result.TaskAttempted = false;
Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Elevated install requested for package [{packageId}] from catalog [{catalogName}]");

var packageManager = _wingetFactory.CreatePackageManager();

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Connecting to catalog [{catalogName}]");
var catalogReference = packageManager.GetPackageCatalogByName(catalogName);
var connectResult = await catalogReference.ConnectAsync();
if (connectResult.Status != ConnectResultStatus.Ok)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to connect to the catalog [{catalogName}] with status {connectResult.Status}");
result.TaskAttempted = false;
return result;
}

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Finding package [{packageId}] in catalog");
var findOptions = CreateFindOptionsForPackageId(packageId);
var findResult = connectResult.PackageCatalog.FindPackages(findOptions);
if (findResult.Status != FindPackagesResultStatus.Ok
|| findResult.Matches.Count < 1
|| findResult.WasLimitExceeded)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to find package. Status={findResult.Status}, Matches Count={findResult.Matches.Count}, LimitReached={findResult.WasLimitExceeded}");
result.TaskAttempted = false;
return result;
}

var packageToInstall = findResult.Matches[0].CatalogPackage;

var installOptions = _wingetFactory.CreateInstallOptions();
installOptions.PackageInstallMode = PackageInstallMode.Silent;

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Initiating install of package {packageId}");
var installResult = await packageManager.InstallPackageAsync(packageToInstall, installOptions);
var extendedErrorCode = installResult.ExtendedErrorCode?.HResult ?? HRESULT.S_OK;

// Contract version 4
var installErrorCode = installResult.GetValueOrDefault(res => res.InstallerErrorCode, HRESULT.S_OK);

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Install finished. Status={installResult.Status}, InstallerErrorCode={installErrorCode}, ExtendedErrorCode={extendedErrorCode}, RebootRequired={installResult.RebootRequired}");
result.TaskAttempted = true;
result.TaskSucceeded = installResult.Status == InstallResultStatus.Ok;
result.RebootRequired = installResult.RebootRequired;
result.Status = (int)installResult.Status;
result.ExtendedErrorCode = extendedErrorCode;
result.InstallerErrorCode = installErrorCode;

return result;
}

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Finding package [{packageId}] in catalog");
var findOptions = CreateFindOptionsForPackageId(packageId);
var findResult = connectResult.PackageCatalog.FindPackages(findOptions);
if (findResult.Status != FindPackagesResultStatus.Ok
|| findResult.Matches.Count < 1
|| findResult.WasLimitExceeded)
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to find package. Status={findResult.Status}, Matches Count={findResult.Matches.Count}, LimitReached={findResult.WasLimitExceeded}");
result.TaskAttempted = false;
return result;
Log.Logger?.ReportError(Log.Component.AppManagement, "Elevated app install failed.", e);
result.TaskSucceeded = false;
}

var packageToInstall = findResult.Matches[0].CatalogPackage;

var installOptions = _wingetFactory.CreateInstallOptions();
installOptions.PackageInstallMode = PackageInstallMode.Silent;

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Initiating install of package {packageId}");
var installResult = await packageManager.InstallPackageAsync(packageToInstall, installOptions);
var extendedErrorCode = installResult.ExtendedErrorCode?.HResult ?? HRESULT.S_OK;

// Contract version 4
var installErrorCode = installResult.GetValueOrDefault(res => res.InstallerErrorCode, HRESULT.S_OK);

Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Install finished. Status={installResult.Status}, InstallerErrorCode={installErrorCode}, ExtendedErrorCode={extendedErrorCode}, RebootRequired={installResult.RebootRequired}");
result.TaskAttempted = true;
result.TaskSucceeded = installResult.Status == InstallResultStatus.Ok;
result.RebootRequired = installResult.RebootRequired;
result.Status = (int)installResult.Status;
result.ExtendedErrorCode = extendedErrorCode;
result.InstallerErrorCode = installErrorCode;

return result;
}).AsAsyncOperation();
}
Expand Down
4 changes: 2 additions & 2 deletions tools/SetupFlow/DevHome.SetupFlow/Models/ConfigureTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task OpenConfigurationSetAsync()
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.Configuration, $"Failed to open configuration set: {e.Message}");
Log.Logger?.ReportError(Log.Component.Configuration, $"Failed to open configuration set.", e);
throw;
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ IAsyncOperation<TaskFinishedState> ISetupTask.Execute()
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.Configuration, $"Failed to apply configuration: {e.Message}");
Log.Logger?.ReportError(Log.Component.Configuration, $"Failed to apply configuration.", e);
return TaskFinishedState.Failure;
}
}).AsAsyncOperation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ IAsyncOperation<TaskFinishedState> ISetupTask.ExecuteAsAdmin(IElevatedComponentF
catch (Exception ex)
{
result = ex.HResult;
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to create Dev Drive. Due to Exception ErrorCode: 0x{ex.HResult:X}, Msg: {ex.Message}");
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to create Dev Drive.", ex);
_actionCenterMessages.PrimaryMessage = _stringResource.GetLocalized(StringResourceKey.DevDriveErrorWithReason, _stringResource.GetLocalizedErrorMsg(ex.HResult, Log.Component.DevDrive));
TelemetryFactory.Get<ITelemetry>().LogException("CreatingDevDriveException", ex);
return TaskFinishedState.Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public IAsyncAction CloneRepositoryAsync(string cloneDestination, IDeveloperId d
}
catch (NameConflictException nameConflictException)
{
Log.Logger?.ReportError("GenericRepository", nameConflictException);
Log.Logger?.ReportError("GenericRepository", string.Empty, nameConflictException);
throw;
}
catch (Exception e)
Expand Down
37 changes: 23 additions & 14 deletions tools/SetupFlow/DevHome.SetupFlow/Models/InstallPackageTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ IAsyncOperation<TaskFinishedState> ISetupTask.Execute()
catch (Exception e)
{
ReportAppInstallFailedEvent();
Log.Logger?.ReportError(Log.Component.AppManagement, $"Exception thrown while installing package: {e.Message}");
Log.Logger?.ReportError(Log.Component.AppManagement, $"Exception thrown while installing package.", e);
return TaskFinishedState.Failure;
}
}).AsAsyncOperation();
Expand All @@ -134,23 +134,32 @@ IAsyncOperation<TaskFinishedState> ISetupTask.ExecuteAsAdmin(IElevatedComponentF
ReportAppSelectedForInstallEvent();
return Task.Run(async () =>
{
Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Starting installation with elevation of package {_package.Id}");
var elevatedTask = elevatedComponentFactory.CreateElevatedInstallTask();
var elevatedResult = await elevatedTask.InstallPackage(_package.Id, _package.CatalogName);
WasInstallSuccessful = elevatedResult.TaskSucceeded;
RequiresReboot = elevatedResult.RebootRequired;
_installResultStatus = (InstallResultStatus)elevatedResult.Status;
_extendedErrorCode = elevatedResult.ExtendedErrorCode;
_installerErrorCode = elevatedResult.InstallerErrorCode;

if (elevatedResult.TaskSucceeded)
try
{
ReportAppInstallSucceededEvent();
return TaskFinishedState.Success;
Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Starting installation with elevation of package {_package.Id}");
var elevatedTask = elevatedComponentFactory.CreateElevatedInstallTask();
var elevatedResult = await elevatedTask.InstallPackage(_package.Id, _package.CatalogName);
WasInstallSuccessful = elevatedResult.TaskSucceeded;
RequiresReboot = elevatedResult.RebootRequired;
_installResultStatus = (InstallResultStatus)elevatedResult.Status;
_extendedErrorCode = elevatedResult.ExtendedErrorCode;
_installerErrorCode = elevatedResult.InstallerErrorCode;

if (elevatedResult.TaskSucceeded)
{
ReportAppInstallSucceededEvent();
return TaskFinishedState.Success;
}
else
{
ReportAppInstallFailedEvent();
return TaskFinishedState.Failure;
}
}
else
catch (Exception e)
{
ReportAppInstallFailedEvent();
Log.Logger?.ReportError(Log.Component.AppManagement, $"Exception thrown while installing package.", e);
return TaskFinishedState.Failure;
}
}).AsAsyncOperation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task ConnectAsync(bool forceReconnect)
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error connecting to catalog reference: {e.Message}");
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error connecting to catalog reference.", e);
throw;
}
finally
Expand All @@ -96,7 +96,7 @@ public async Task<IList<IWinGetPackage>> SearchAsync(string query, uint limit)
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error searching for packages: {e.Message}");
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error searching for packages.", e);
throw;
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ public async Task<IList<IWinGetPackage>> GetPackagesAsync(ISet<string> packageId
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error getting packages: {e.Message}");
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error getting packages.", e);
throw;
}
}
Expand Down
8 changes: 4 additions & 4 deletions tools/SetupFlow/DevHome.SetupFlow/Services/DevDriveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public IEnumerable<IDevDrive> GetAllDevDrivesThatExistOnSystem()
catch (Exception ex)
{
// Log then return empty list, don't show the user their existing dev drive. Not catastrophic failure.
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed Get existing Dev Drives. ErrorCode: {ex.HResult}, Msg: {ex.Message}");
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to get existing Dev Drives.", ex);
return new List<IDevDrive>();
}
}
Expand All @@ -272,7 +272,7 @@ private IDevDrive GetDevDriveWithDefaultInfo()
}
catch (Exception ex)
{
Log.Logger?.ReportError(Log.Component.DevDrive, $"Unable to get available Free Space for {root}: ErrorCode: {ex.HResult}, Msg: {ex.Message}");
Log.Logger?.ReportError(Log.Component.DevDrive, $"Unable to get available Free Space for {root}.", ex);
validationSuccessful = false;
}

Expand Down Expand Up @@ -364,7 +364,7 @@ public ISet<DevDriveValidationResult> GetDevDriveValidationResults(IDevDrive dev
}
catch (Exception ex)
{
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to validate selected Drive letter ({devDrive.DriveLocation.FirstOrDefault()}). ErrorCode: {ex.HResult}, Msg: {ex.Message}");
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to validate selected Drive letter ({devDrive.DriveLocation.FirstOrDefault()}).", ex);
returnSet.Add(DevDriveValidationResult.DriveLetterNotAvailable);
}

Expand Down Expand Up @@ -397,7 +397,7 @@ public IList<char> GetAvailableDriveLetters(char? usedLetterToKeepInList = null)
}
catch (Exception ex)
{
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to get Available Drive letters. ErrorCode: {ex.HResult}, Msg: {ex.Message}");
Log.Logger?.ReportError(Log.Component.DevDrive, $"Failed to get Available Drive letters.", ex);
}

return driveLetterSet.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private async Task<PackageCatalog> LoadCatalogAsync(JsonWinGetPackageCatalog jso
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error loading packages from winget catalog: {e.Message}");
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error loading packages from winget catalog.", e);
}

return null;
Expand All @@ -147,7 +147,7 @@ private async Task<IRandomAccessStream> GetJsonApplicationIconAsync(JsonWinGetPa
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to get icon for JSON package {package.Id}", e);
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to get icon for JSON package {package.Id}.", e);
}

Log.Logger?.ReportWarn(Log.Component.AppManagement, $"No icon found for JSON package {package.Id}. A default one will be provided.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public async override Task<IList<PackageCatalog>> LoadCatalogsAsync()
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error loading packages from winget restore catalog: {e.Message}");
Log.Logger?.ReportError(Log.Component.AppManagement, $"Error loading packages from winget restore catalog.", e);
}

return result;
Expand Down
Loading