commit ec46ec319b5b76c8552b3482418101c5653dede5
parent 24511e4f3d053d4f34f3157950e4e3a20599f039
Author: JohnMcPMS <johnmcp@microsoft.com>
Date: Thu, 27 Feb 2025 08:56:48 -0800
Don't let unset error codes be thrown (#5255)
## Change
Some of the `http_exception`s coming through this function don't have
error codes set. Throwing that success code causes WIL to fail fast.
Diffstat:
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/src/AppInstallerCommonCore/HttpClientHelper.cpp b/src/AppInstallerCommonCore/HttpClientHelper.cpp
@@ -244,6 +244,23 @@ namespace AppInstaller::Http
[[noreturn]] void HttpClientHelper::RethrowAsWilException(const web::http::http_exception& exception)
{
- THROW_WIN32_MSG(exception.error_code().value(), "%hs", exception.what());
+ // Some http_exceptions have no error code; default to REST internal error.
+ HRESULT toThrow = APPINSTALLER_CLI_ERROR_RESTAPI_INTERNAL_ERROR;
+
+ // 99% of the time this code comes from GetLastError.
+ // In a few cases it will be 400; as in the HTTP status code.
+ // Since that is the one case that http_client_winhttp.cpp uses, we map it specifically.
+ // In the event that this makes no sense, ERROR_THREAD_MODE_ALREADY_BACKGROUND is Win32 error 400.
+ int errorValue = exception.error_code().value();
+ if (errorValue == web::http::status_codes::BadRequest)
+ {
+ toThrow = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_HTTP, web::http::status_codes::BadRequest);
+ }
+ else if (errorValue)
+ {
+ toThrow = HRESULT_FROM_WIN32(errorValue);
+ }
+
+ THROW_HR_MSG(toThrow, "%hs", exception.what());
}
}