Error: Strip trailing whitespace from Windows errors

Backport of c85e743573
This commit is contained in:
Stenzek 2024-05-15 08:13:26 +10:00 committed by Connor McLaughlin
parent cfecbf53aa
commit 49a17b3a2e
1 changed files with 9 additions and 2 deletions

View File

@ -8,6 +8,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <cwctype>
#include <type_traits> #include <type_traits>
// Platform-specific includes // Platform-specific includes
@ -108,8 +109,11 @@ void Error::SetWin32(std::string_view prefix, unsigned long err)
m_type = Type::Win32; m_type = Type::Win32;
WCHAR buf[128]; WCHAR buf[128];
const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf, DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf,
static_cast<DWORD>(std::size(buf)), nullptr); static_cast<DWORD>(std::size(buf)), nullptr);
while (r > 0 && std::iswspace(buf[r - 1]))
r--;
if (r > 0) if (r > 0)
{ {
m_description = m_description =
@ -143,8 +147,11 @@ void Error::SetHResult(std::string_view prefix, long err)
m_type = Type::HResult; m_type = Type::HResult;
WCHAR buf[128]; WCHAR buf[128];
const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf, DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf,
static_cast<DWORD>(std::size(buf)), nullptr); static_cast<DWORD>(std::size(buf)), nullptr);
while (r > 0 && std::iswspace(buf[r - 1]))
r--;
if (r > 0) if (r > 0)
{ {
m_description = fmt::format("{}HRESULT {:08X}: {}", prefix, static_cast<unsigned>(err), m_description = fmt::format("{}HRESULT {:08X}: {}", prefix, static_cast<unsigned>(err),