pcsx2/common/Error.cpp

273 lines
5.3 KiB
C++
Raw Normal View History

2024-04-01 10:48:39 +00:00
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
// SPDX-License-Identifier: LGPL-3.0+
2022-12-28 11:13:26 +00:00
#include "Error.h"
2024-04-01 10:48:39 +00:00
#include "StringUtil.h"
#include "fmt/format.h"
2022-12-28 11:13:26 +00:00
#include <cstdlib>
#include <cstring>
#include <type_traits>
// Platform-specific includes
#if defined(_WIN32)
#include "RedtapeWindows.h"
static_assert(std::is_same<DWORD, unsigned long>::value, "DWORD is unsigned long");
static_assert(std::is_same<HRESULT, long>::value, "HRESULT is long");
#endif
Error::Error() = default;
Error::Error(const Error& c) = default;
Error::Error(Error&& e) = default;
Error::~Error() = default;
void Error::Clear()
{
m_description = {};
}
void Error::SetErrno(int err)
2024-04-01 10:48:39 +00:00
{
SetErrno(std::string_view(), err);
}
void Error::SetErrno(std::string_view prefix, int err)
2022-12-28 11:13:26 +00:00
{
m_type = Type::Errno;
#ifdef _MSC_VER
char buf[128];
2023-09-16 15:05:33 +00:00
if (strerror_s(buf, sizeof(buf), err) == 0)
2024-04-01 10:48:39 +00:00
m_description = fmt::format("{}errno {}: {}", prefix, err, buf);
2022-12-28 11:13:26 +00:00
else
2024-04-01 10:48:39 +00:00
m_description = fmt::format("{}errno {}: <Could not get error message>", prefix, err);
2022-12-28 11:13:26 +00:00
#else
const char* buf = std::strerror(err);
if (buf)
2024-04-01 10:48:39 +00:00
m_description = fmt::format("{}errno {}: {}", prefix, err, buf);
2022-12-28 11:13:26 +00:00
else
2024-04-01 10:48:39 +00:00
m_description = fmt::format("{}errno {}: <Could not get error message>", prefix, err);
2022-12-28 11:13:26 +00:00
#endif
}
void Error::SetErrno(Error* errptr, int err)
{
if (errptr)
errptr->SetErrno(err);
}
2024-04-01 10:48:39 +00:00
void Error::SetErrno(Error* errptr, std::string_view prefix, int err)
{
if (errptr)
errptr->SetErrno(prefix, err);
}
2022-12-28 11:13:26 +00:00
void Error::SetString(std::string description)
{
m_type = Type::User;
m_description = std::move(description);
}
2024-04-01 10:48:39 +00:00
void Error::SetStringView(std::string_view description)
{
m_type = Type::User;
m_description = std::string(description);
}
2022-12-28 11:13:26 +00:00
void Error::SetString(Error* errptr, std::string description)
{
if (errptr)
errptr->SetString(std::move(description));
}
2024-04-01 10:48:39 +00:00
void Error::SetStringView(Error* errptr, std::string_view description)
{
if (errptr)
errptr->SetStringView(std::move(description));
}
2022-12-28 11:13:26 +00:00
#ifdef _WIN32
2024-04-01 10:48:39 +00:00
2022-12-28 11:13:26 +00:00
void Error::SetWin32(unsigned long err)
2024-04-01 10:48:39 +00:00
{
SetWin32(std::string_view(), err);
}
void Error::SetWin32(std::string_view prefix, unsigned long err)
2022-12-28 11:13:26 +00:00
{
m_type = Type::Win32;
2024-04-01 10:48:39 +00:00
WCHAR buf[128];
const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf,
static_cast<DWORD>(std::size(buf)), nullptr);
2022-12-28 11:13:26 +00:00
if (r > 0)
2024-04-01 10:48:39 +00:00
{
m_description =
fmt::format("{}Win32 Error {}: {}", prefix, err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
}
2022-12-28 11:13:26 +00:00
else
2024-04-01 10:48:39 +00:00
{
m_description = fmt::format("{}Win32 Error {}: <Could not resolve system error ID>", prefix, err);
}
2022-12-28 11:13:26 +00:00
}
void Error::SetWin32(Error* errptr, unsigned long err)
{
if (errptr)
errptr->SetWin32(err);
}
2024-04-01 10:48:39 +00:00
void Error::SetWin32(Error* errptr, std::string_view prefix, unsigned long err)
{
if (errptr)
errptr->SetWin32(prefix, err);
}
2022-12-28 11:13:26 +00:00
void Error::SetHResult(long err)
2024-04-01 10:48:39 +00:00
{
SetHResult(std::string_view(), err);
}
void Error::SetHResult(std::string_view prefix, long err)
2022-12-28 11:13:26 +00:00
{
m_type = Type::HResult;
2024-04-01 10:48:39 +00:00
WCHAR buf[128];
const DWORD r = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_USER_DEFAULT, buf,
static_cast<DWORD>(std::size(buf)), nullptr);
2022-12-28 11:13:26 +00:00
if (r > 0)
2024-04-01 10:48:39 +00:00
{
m_description =
fmt::format("{}HRESULT {:08X}: {}", prefix, err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
}
2022-12-28 11:13:26 +00:00
else
2024-04-01 10:48:39 +00:00
{
m_description = fmt::format("{}HRESULT {:08X}: <Could not resolve system error ID>", prefix, err);
}
2022-12-28 11:13:26 +00:00
}
void Error::SetHResult(Error* errptr, long err)
{
if (errptr)
errptr->SetHResult(err);
}
2024-04-01 10:48:39 +00:00
void Error::SetHResult(Error* errptr, std::string_view prefix, long err)
{
if (errptr)
errptr->SetHResult(prefix, err);
}
2022-12-28 11:13:26 +00:00
#endif
void Error::SetSocket(int err)
2024-04-01 10:48:39 +00:00
{
SetSocket(std::string_view(), err);
}
void Error::SetSocket(std::string_view prefix, int err)
2022-12-28 11:13:26 +00:00
{
// Socket errors are win32 errors on windows
#ifdef _WIN32
2024-04-01 10:48:39 +00:00
SetWin32(prefix, err);
2022-12-28 11:13:26 +00:00
#else
2024-04-01 10:48:39 +00:00
SetErrno(prefix, err);
2022-12-28 11:13:26 +00:00
#endif
m_type = Type::Socket;
}
void Error::SetSocket(Error* errptr, int err)
{
if (errptr)
errptr->SetSocket(err);
}
2024-04-01 10:48:39 +00:00
void Error::SetSocket(Error* errptr, std::string_view prefix, int err)
{
if (errptr)
errptr->SetSocket(prefix, err);
}
2022-12-28 11:13:26 +00:00
Error Error::CreateNone()
{
return Error();
}
Error Error::CreateErrno(int err)
{
Error ret;
ret.SetErrno(err);
return ret;
}
Error Error::CreateSocket(int err)
{
Error ret;
ret.SetSocket(err);
return ret;
}
Error Error::CreateString(std::string description)
{
Error ret;
ret.SetString(std::move(description));
return ret;
}
#ifdef _WIN32
Error Error::CreateWin32(unsigned long err)
{
Error ret;
ret.SetWin32(err);
return ret;
}
Error Error::CreateHResult(long err)
{
Error ret;
ret.SetHResult(err);
return ret;
}
#endif
2024-04-01 10:48:39 +00:00
void Error::AddPrefix(std::string_view prefix)
{
m_description.insert(0, prefix);
}
void Error::AddSuffix(std::string_view suffix)
{
m_description.append(suffix);
}
void Error::AddPrefix(Error* errptr, std::string_view prefix)
{
if (errptr)
errptr->AddPrefix(prefix);
}
void Error::AddSuffix(Error* errptr, std::string_view prefix)
{
if (errptr)
errptr->AddSuffix(prefix);
}
2022-12-28 11:13:26 +00:00
Error& Error::operator=(const Error& e) = default;
Error& Error::operator=(Error&& e) = default;
bool Error::operator==(const Error& e) const
{
return (m_type == e.m_type && m_description == e.m_description);
}
bool Error::operator!=(const Error& e) const
{
return (m_type != e.m_type || m_description != e.m_description);
}