Improve the quality of the WinHTTP transport error message. (#3330)

* Improved HTTP transport error message with Win32 error

* Use std::unique_ptr to manage lifetime of error message
This commit is contained in:
Larry Osterman 2022-02-08 12:32:35 -08:00 committed by GitHub
parent b728939fce
commit 3642552d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -201,8 +201,29 @@ std::string GetHeadersAsString(Azure::Core::Http::Request const& request)
void GetErrorAndThrow(const std::string& exceptionMessage)
{
DWORD error = GetLastError();
throw Azure::Core::Http::TransportException(
exceptionMessage + " Error Code: " + std::to_string(error) + ".");
std::string errorMessage = exceptionMessage + " Error Code: " + std::to_string(error);
char* errorMsg = nullptr;
if (FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER,
GetModuleHandle("winhttp.dll"),
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&errorMsg),
0,
nullptr)
!= 0)
{
// Use a unique_ptr to manage the lifetime of errorMsg.
std::unique_ptr<char, decltype(&LocalFree)> errorString(errorMsg, &LocalFree);
errorMsg = nullptr;
errorMessage += ": ";
errorMessage += errorString.get();
}
errorMessage += '.';
throw Azure::Core::Http::TransportException(errorMessage);
}
void WinHttpTransport::CreateSessionHandle(std::unique_ptr<_detail::HandleManager>& handleManager)