Skip to content

Commit 7f00ea8

Browse files
authored
Convert cpprestsdk to WIL exception for better handling (#5195)
## Change To better expose the underlying result code, convert the `http_exception` to a `ResultException` so that the HRESULT can be properly extracted. CP #5188
1 parent 36b1518 commit 7f00ea8

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

Diff for: src/AppInstallerCommonCore/HttpClientHelper.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace AppInstaller::Http
9696
const web::json::value& body,
9797
const HttpClientHelper::HttpRequestHeaders& headers,
9898
const HttpClientHelper::HttpRequestHeaders& authHeaders,
99-
const HttpResponseHandler& customHandler) const
99+
const HttpResponseHandler& customHandler) const try
100100
{
101101
web::http::http_response httpResponse;
102102
Post(uri, body, headers, authHeaders).then([&httpResponse](const web::http::http_response& response)
@@ -115,6 +115,10 @@ namespace AppInstaller::Http
115115

116116
return ValidateAndExtractResponse(httpResponse);
117117
}
118+
catch (web::http::http_exception& exception)
119+
{
120+
RethrowAsWilException(exception);
121+
}
118122

119123
pplx::task<web::http::http_response> HttpClientHelper::Get(
120124
const utility::string_t& uri,
@@ -148,7 +152,7 @@ namespace AppInstaller::Http
148152
const utility::string_t& uri,
149153
const HttpClientHelper::HttpRequestHeaders& headers,
150154
const HttpClientHelper::HttpRequestHeaders& authHeaders,
151-
const HttpResponseHandler& customHandler) const
155+
const HttpResponseHandler& customHandler) const try
152156
{
153157
web::http::http_response httpResponse;
154158
Get(uri, headers, authHeaders).then([&httpResponse](const web::http::http_response& response)
@@ -167,6 +171,10 @@ namespace AppInstaller::Http
167171

168172
return ValidateAndExtractResponse(httpResponse);
169173
}
174+
catch (web::http::http_exception& exception)
175+
{
176+
RethrowAsWilException(exception);
177+
}
170178

171179
void HttpClientHelper::SetPinningConfiguration(const Certificates::PinningConfiguration& configuration)
172180
{
@@ -233,4 +241,9 @@ namespace AppInstaller::Http
233241

234242
return response.extract_json().get();
235243
}
244+
245+
[[noreturn]] void HttpClientHelper::RethrowAsWilException(const web::http::http_exception& exception)
246+
{
247+
THROW_WIN32_MSG(exception.error_code().value(), "%hs", exception.what());
248+
}
236249
}

Diff for: src/AppInstallerCommonCore/Public/winget/HttpClientHelper.h

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ namespace AppInstaller::Http
4444
private:
4545
web::http::client::http_client GetClient(const utility::string_t& uri) const;
4646

47+
// Translates a cpprestsdk http_exception to a WIL exception.
48+
static void RethrowAsWilException(const web::http::http_exception& exception);
49+
4750
std::shared_ptr<web::http::http_pipeline_stage> m_defaultRequestHandlerStage;
4851
web::http::client::http_client_config m_clientConfig;
4952
};

Diff for: src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/Common/FinderCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private PackageCatalog GetPackageCatalog(CompositeSearchBehavior behavior)
150150
}
151151
else
152152
{
153-
throw new CatalogConnectException();
153+
throw new CatalogConnectException(result.ExtendedErrorCode);
154154
}
155155
}
156156

Diff for: src/PowerShell/Microsoft.WinGet.Client.Engine/Exceptions/CatalogConnectException.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// -----------------------------------------------------------------------------
1+
// -----------------------------------------------------------------------------
22
// <copyright file="CatalogConnectException.cs" company="Microsoft Corporation">
33
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
44
// </copyright>
@@ -19,8 +19,9 @@ public class CatalogConnectException : RuntimeException
1919
/// <summary>
2020
/// Initializes a new instance of the <see cref="CatalogConnectException"/> class.
2121
/// </summary>
22-
public CatalogConnectException()
23-
: base(Resources.CatalogConnectExceptionMessage)
22+
/// <param name="inner">The exception that lead to this one.</param>
23+
public CatalogConnectException(Exception inner)
24+
: base(Resources.CatalogConnectExceptionMessage, inner)
2425
{
2526
}
2627
}

0 commit comments

Comments
 (0)