From d1bfd1abedf737060ddeda5d35ee191051381772 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 20 Feb 2025 14:12:14 +0100 Subject: [PATCH] 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 --- include/WinStd/WinHTTP.h | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/include/WinStd/WinHTTP.h b/include/WinStd/WinHTTP.h index b9601600..b14d33b9 100644 --- a/include/WinStd/WinHTTP.h +++ b/include/WinStd/WinHTTP.h @@ -141,4 +141,87 @@ namespace winstd }; /// @} + + /// \addtogroup WinStdExceptions + /// @{ + + /// + /// WinHTTP error + /// + class http_error : public num_runtime_error + { + public: + /// + /// Constructs an exception + /// + /// \param[in] num WinHTTP error code + /// + http_error(_In_ error_type num) : num_runtime_error(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(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(num, std::string(msg) + ": " + message(num)) + {} + + /// + /// Constructs an exception using `GetLastError()` + /// + http_error() : num_runtime_error(GetLastError(), message(GetLastError())) + {} + + /// + /// Constructs an exception using `GetLastError()` + /// + /// \param[in] msg Error message + /// + http_error(_In_ const std::string& msg) : num_runtime_error(GetLastError(), msg + ": " + message(GetLastError())) + {} + + /// + /// Constructs an exception using `GetLastError()` + /// + /// \param[in] msg Error message + /// + http_error(_In_z_ const char *msg) : num_runtime_error(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; + } + }; + + /// @} }