Error: Strip trailing whitespace from Windows errors

Backport of https://github.com/stenzek/duckstation/commit/c85e74357356fd0d9b5b0c7711cf514ef47feeea
This commit is contained in:
Stenzek
2024-05-17 11:57:38 +10:00
committed by Connor McLaughlin
parent cfecbf53aa
commit 49a17b3a2e
+9 -2
View File
@@ -8,6 +8,7 @@
#include <cstdlib>
#include <cstring>
#include <cwctype>
#include <type_traits>
// Platform-specific includes
@@ -108,8 +109,11 @@ void Error::SetWin32(std::string_view prefix, unsigned long err)
m_type = Type::Win32;
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);
while (r > 0 && std::iswspace(buf[r - 1]))
r--;
if (r > 0)
{
m_description =
@@ -143,8 +147,11 @@ void Error::SetHResult(std::string_view prefix, long err)
m_type = Type::HResult;
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);
while (r > 0 && std::iswspace(buf[r - 1]))
r--;
if (r > 0)
{
m_description = fmt::format("{}HRESULT {:08X}: {}", prefix, static_cast<unsigned>(err),