Duckstation/src/common/error.cpp

190 lines
3.7 KiB
C++
Raw Normal View History

2023-08-19 13:40:36 +00:00
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
2021-03-18 15:51:39 +00:00
#include "error.h"
2023-08-19 13:40:36 +00:00
#include "string_util.h"
2021-03-18 15:51:39 +00:00
#include <cstdlib>
#include <cstring>
#include <type_traits>
2023-08-19 13:40:36 +00:00
#include "fmt/format.h"
2021-03-18 15:51:39 +00:00
#if defined(_WIN32)
#include "windows_headers.h"
#endif
2023-08-19 13:40:36 +00:00
Error::Error() = default;
2021-03-18 15:51:39 +00:00
2023-08-19 13:40:36 +00:00
Error::Error(const Error& c) = default;
2021-03-18 15:51:39 +00:00
2023-08-19 13:40:36 +00:00
Error::Error(Error&& e) = default;
2021-03-18 15:51:39 +00:00
Error::~Error() = default;
void Error::Clear()
{
2023-08-19 13:40:36 +00:00
m_description = {};
2021-03-18 15:51:39 +00:00
}
void Error::SetErrno(int err)
{
m_type = Type::Errno;
#ifdef _MSC_VER
2023-08-19 13:40:36 +00:00
char buf[128];
2023-09-16 14:51:07 +00:00
if (strerror_s(buf, sizeof(buf), err) == 0)
2023-08-19 13:40:36 +00:00
m_description = fmt::format("errno {}: {}", err, buf);
2021-03-18 15:51:39 +00:00
else
2023-08-19 13:40:36 +00:00
m_description = fmt::format("errno {}: <Could not get error message>", err);
2021-03-18 15:51:39 +00:00
#else
2023-08-19 13:40:36 +00:00
const char* buf = std::strerror(err);
if (buf)
m_description = fmt::format("errno {}: {}", err, buf);
else
m_description = fmt::format("errno {}: <Could not get error message>", err);
2021-03-18 15:51:39 +00:00
#endif
}
2023-08-19 13:40:36 +00:00
void Error::SetErrno(Error* errptr, int err)
2021-03-18 15:51:39 +00:00
{
2023-08-19 13:40:36 +00:00
if (errptr)
errptr->SetErrno(err);
2021-03-18 15:51:39 +00:00
}
2023-08-19 13:40:36 +00:00
void Error::SetString(std::string description)
2021-03-18 15:51:39 +00:00
{
m_type = Type::User;
2023-08-19 13:40:36 +00:00
m_description = std::move(description);
2021-03-18 15:51:39 +00:00
}
2023-08-19 13:40:36 +00:00
void Error::SetString(Error* errptr, std::string description)
2021-03-18 15:51:39 +00:00
{
2023-08-19 13:40:36 +00:00
if (errptr)
errptr->SetString(std::move(description));
2021-03-18 15:51:39 +00:00
}
#ifdef _WIN32
void Error::SetWin32(unsigned long err)
{
m_type = Type::Win32;
2023-08-19 13:40:36 +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);
2021-03-18 15:51:39 +00:00
if (r > 0)
{
2023-08-19 13:40:36 +00:00
m_description =
fmt::format("Win32 Error {}: {}", err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
2021-03-18 15:51:39 +00:00
}
else
{
2023-08-19 13:40:36 +00:00
m_description = fmt::format("Win32 Error {}: <Could not resolve system error ID>", err);
2021-03-18 15:51:39 +00:00
}
}
2023-08-19 13:40:36 +00:00
void Error::SetWin32(Error* errptr, unsigned long err)
{
if (errptr)
errptr->SetWin32(err);
}
2021-03-18 15:51:39 +00:00
void Error::SetHResult(long err)
{
m_type = Type::HResult;
2023-08-19 13:40:36 +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);
2021-03-18 15:51:39 +00:00
if (r > 0)
{
2023-08-19 13:40:36 +00:00
m_description =
fmt::format("HRESULT {:08X}: {}", err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
2021-03-18 15:51:39 +00:00
}
else
{
2023-08-19 13:40:36 +00:00
m_description = fmt::format("HRESULT {:08X}: <Could not resolve system error ID>", err);
2021-03-18 15:51:39 +00:00
}
}
2023-08-19 13:40:36 +00:00
void Error::SetHResult(Error* errptr, long err)
2021-03-18 15:51:39 +00:00
{
2023-08-19 13:40:36 +00:00
if (errptr)
errptr->SetHResult(err);
2021-03-18 15:51:39 +00:00
}
2023-08-19 13:40:36 +00:00
#endif
2021-03-18 15:51:39 +00:00
2023-08-19 13:40:36 +00:00
void Error::SetSocket(int err)
2021-03-18 15:51:39 +00:00
{
2023-08-19 13:40:36 +00:00
// Socket errors are win32 errors on windows
#ifdef _WIN32
SetWin32(err);
#else
SetErrno(err);
#endif
m_type = Type::Socket;
2021-03-18 15:51:39 +00:00
}
2023-08-19 13:40:36 +00:00
void Error::SetSocket(Error* errptr, int err)
2021-03-18 15:51:39 +00:00
{
2023-08-19 13:40:36 +00:00
if (errptr)
errptr->SetSocket(err);
2021-03-18 15:51:39 +00:00
}
2023-08-19 13:40:36 +00:00
Error Error::CreateNone()
2021-03-18 15:51:39 +00:00
{
2023-08-19 13:40:36 +00:00
return Error();
2021-03-18 15:51:39 +00:00
}
2023-08-19 13:40:36 +00:00
Error Error::CreateErrno(int err)
2021-03-18 15:51:39 +00:00
{
Error ret;
2023-08-19 13:40:36 +00:00
ret.SetErrno(err);
2021-03-18 15:51:39 +00:00
return ret;
}
2023-08-19 13:40:36 +00:00
Error Error::CreateSocket(int err)
2021-03-18 15:51:39 +00:00
{
Error ret;
2023-08-19 13:40:36 +00:00
ret.SetSocket(err);
2021-03-18 15:51:39 +00:00
return ret;
}
2023-08-19 13:40:36 +00:00
Error Error::CreateString(std::string description)
2021-03-18 15:51:39 +00:00
{
Error ret;
2023-08-19 13:40:36 +00:00
ret.SetString(std::move(description));
2021-03-18 15:51:39 +00:00
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
2023-08-19 13:40:36 +00:00
Error& Error::operator=(const Error& e) = default;
Error& Error::operator=(Error&& e) = default;
2021-03-18 15:51:39 +00:00
bool Error::operator==(const Error& e) const
{
2023-08-19 13:40:36 +00:00
return (m_type == e.m_type && m_description == e.m_description);
2021-03-18 15:51:39 +00:00
}
bool Error::operator!=(const Error& e) const
{
2023-08-19 13:40:36 +00:00
return (m_type != e.m_type || m_description != e.m_description);
}