winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit c32d376b8bc428eacf5669d3d08585b52bd8b2e4
parent 027adb194f91f8d0036dc87df97436b27a2cb096
Author: JohnMcPMS <johnmcp@microsoft.com>
Date:   Mon, 10 Feb 2025 14:07:05 -0800

Convert cpprestsdk to WIL exception for better handling (#5188)

## Change
To better expose the underlying result code, convert the
`http_exception` to a `ResultException` so that the HRESULT can be
properly extracted.
Diffstat:
Msrc/AppInstallerCommonCore/HttpClientHelper.cpp | 17+++++++++++++++--
Msrc/AppInstallerCommonCore/Public/winget/HttpClientHelper.h | 3+++
Msrc/PowerShell/Microsoft.WinGet.Client.Engine/Commands/Common/FinderCommand.cs | 2+-
Msrc/PowerShell/Microsoft.WinGet.Client.Engine/Exceptions/CatalogConnectException.cs | 7++++---
4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/src/AppInstallerCommonCore/HttpClientHelper.cpp b/src/AppInstallerCommonCore/HttpClientHelper.cpp @@ -96,7 +96,7 @@ namespace AppInstaller::Http const web::json::value& body, const HttpClientHelper::HttpRequestHeaders& headers, const HttpClientHelper::HttpRequestHeaders& authHeaders, - const HttpResponseHandler& customHandler) const + const HttpResponseHandler& customHandler) const try { web::http::http_response httpResponse; Post(uri, body, headers, authHeaders).then([&httpResponse](const web::http::http_response& response) @@ -115,6 +115,10 @@ namespace AppInstaller::Http return ValidateAndExtractResponse(httpResponse); } + catch (web::http::http_exception& exception) + { + RethrowAsWilException(exception); + } pplx::task<web::http::http_response> HttpClientHelper::Get( const utility::string_t& uri, @@ -148,7 +152,7 @@ namespace AppInstaller::Http const utility::string_t& uri, const HttpClientHelper::HttpRequestHeaders& headers, const HttpClientHelper::HttpRequestHeaders& authHeaders, - const HttpResponseHandler& customHandler) const + const HttpResponseHandler& customHandler) const try { web::http::http_response httpResponse; Get(uri, headers, authHeaders).then([&httpResponse](const web::http::http_response& response) @@ -167,6 +171,10 @@ namespace AppInstaller::Http return ValidateAndExtractResponse(httpResponse); } + catch (web::http::http_exception& exception) + { + RethrowAsWilException(exception); + } void HttpClientHelper::SetPinningConfiguration(const Certificates::PinningConfiguration& configuration) { @@ -233,4 +241,9 @@ namespace AppInstaller::Http return response.extract_json().get(); } + + [[noreturn]] void HttpClientHelper::RethrowAsWilException(const web::http::http_exception& exception) + { + THROW_WIN32_MSG(exception.error_code().value(), "%hs", exception.what()); + } } diff --git a/src/AppInstallerCommonCore/Public/winget/HttpClientHelper.h b/src/AppInstallerCommonCore/Public/winget/HttpClientHelper.h @@ -44,6 +44,9 @@ namespace AppInstaller::Http private: web::http::client::http_client GetClient(const utility::string_t& uri) const; + // Translates a cpprestsdk http_exception to a WIL exception. + static void RethrowAsWilException(const web::http::http_exception& exception); + std::shared_ptr<web::http::http_pipeline_stage> m_defaultRequestHandlerStage; web::http::client::http_client_config m_clientConfig; }; diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/Common/FinderCommand.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/Common/FinderCommand.cs @@ -150,7 +150,7 @@ namespace Microsoft.WinGet.Client.Engine.Commands.Common } else { - throw new CatalogConnectException(); + throw new CatalogConnectException(result.ExtendedErrorCode); } } diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Exceptions/CatalogConnectException.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Exceptions/CatalogConnectException.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // <copyright file="CatalogConnectException.cs" company="Microsoft Corporation"> // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // </copyright> @@ -19,8 +19,9 @@ namespace Microsoft.WinGet.Client.Engine.Exceptions /// <summary> /// Initializes a new instance of the <see cref="CatalogConnectException"/> class. /// </summary> - public CatalogConnectException() - : base(Resources.CatalogConnectExceptionMessage) + /// <param name="inner">The exception that lead to this one.</param> + public CatalogConnectException(Exception inner) + : base(Resources.CatalogConnectExceptionMessage, inner) { } }