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:
parent
b728939fce
commit
3642552d0b
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user