Skip to content

Commit 2ef20bf

Browse files
authored
Don't let unset error codes be thrown (#5257)
## 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. CP from #5255
1 parent 4e6336a commit 2ef20bf

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/AppInstallerCommonCore/HttpClientHelper.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ namespace AppInstaller::Http
244244

245245
[[noreturn]] void HttpClientHelper::RethrowAsWilException(const web::http::http_exception& exception)
246246
{
247-
THROW_WIN32_MSG(exception.error_code().value(), "%hs", exception.what());
247+
// Some http_exceptions have no error code; default to REST internal error.
248+
HRESULT toThrow = APPINSTALLER_CLI_ERROR_RESTAPI_INTERNAL_ERROR;
249+
250+
// 99% of the time this code comes from GetLastError.
251+
// In a few cases it will be 400; as in the HTTP status code.
252+
// Since that is the one case that http_client_winhttp.cpp uses, we map it specifically.
253+
// In the event that this makes no sense, ERROR_THREAD_MODE_ALREADY_BACKGROUND is Win32 error 400.
254+
int errorValue = exception.error_code().value();
255+
if (errorValue == web::http::status_codes::BadRequest)
256+
{
257+
toThrow = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_HTTP, web::http::status_codes::BadRequest);
258+
}
259+
else if (errorValue)
260+
{
261+
toThrow = HRESULT_FROM_WIN32(errorValue);
262+
}
263+
264+
THROW_HR_MSG(toThrow, "%hs", exception.what());
248265
}
249266
}

0 commit comments

Comments
 (0)