WinHTTP: Add http_error

WinHTTP errors don't get resolved by FormatMessage by default. We need
to format them using WINHTTP.DLL resources.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2025-02-20 14:12:14 +01:00
parent 3053b98ab8
commit d1bfd1abed

View File

@ -141,4 +141,87 @@ namespace winstd
};
/// @}
/// \addtogroup WinStdExceptions
/// @{
///
/// WinHTTP error
///
class http_error : public num_runtime_error<DWORD>
{
public:
///
/// Constructs an exception
///
/// \param[in] num WinHTTP error code
///
http_error(_In_ error_type num) : num_runtime_error<DWORD>(num, message(num))
{}
///
/// Constructs an exception
///
/// \param[in] num WinHTTP error code
/// \param[in] msg Error message
///
http_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
{}
///
/// Constructs an exception
///
/// \param[in] num WinHTTP error code
/// \param[in] msg Error message
///
http_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<DWORD>(num, std::string(msg) + ": " + message(num))
{}
///
/// Constructs an exception using `GetLastError()`
///
http_error() : num_runtime_error<DWORD>(GetLastError(), message(GetLastError()))
{}
///
/// Constructs an exception using `GetLastError()`
///
/// \param[in] msg Error message
///
http_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
{}
///
/// Constructs an exception using `GetLastError()`
///
/// \param[in] msg Error message
///
http_error(_In_z_ const char *msg) : num_runtime_error<DWORD>(GetLastError(), std::string(msg) + ": " + message(GetLastError()))
{}
protected:
///
/// Returns a user-readable WinHTTP error message.
/// As std::exception messages may only be char*, we use UTF-8 by convention.
///
/// \sa [FormatMessage function](https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-formatmessage)
///
static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
{
last_error_saver last_error_save;
std::wstring wstr;
winstd::library winhttp(LoadLibraryW(L"WINHTTP.DLL"));
if (WINHTTP_ERROR_BASE < num && num <= WINHTTP_ERROR_LAST && winhttp && FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, (HMODULE)winhttp, num, dwLanguageId, wstr, NULL) ||
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;
}
};
/// @}
}