Skip to content

Commit

Permalink
Convert cpprestsdk to WIL exception for better handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Feb 6, 2025
1 parent f9812eb commit 8d124bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/AppInstallerCommonCore/HttpClientHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -233,4 +241,9 @@ namespace AppInstaller::Http

return response.extract_json().get();
}

[[noreturn]] void HttpClientHelper::RethrowAsWilException(web::http::http_exception& exception)
{
THROW_WIN32_MSG(exception.error_code().value(), "%hs", exception.what());
}
}
3 changes: 3 additions & 0 deletions src/AppInstallerCommonCore/Public/winget/HttpClientHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(web::http::http_exception& exception);

std::shared_ptr<web::http::http_pipeline_stage> m_defaultRequestHandlerStage;
web::http::client::http_client_config m_clientConfig;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private PackageCatalog GetPackageCatalog(CompositeSearchBehavior behavior)
}
else
{
throw new CatalogConnectException();
throw new CatalogConnectException(result.ExtendedErrorCode);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="CatalogConnectException.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand All @@ -19,8 +19,9 @@ public class CatalogConnectException : RuntimeException
/// <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)
{
}
}
Expand Down

0 comments on commit 8d124bb

Please sign in to comment.