This commit is contained in:
Mars 2025-04-28 04:14:13 -04:00
parent 24b6a72614
commit 1a2fba7fb8
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
29 changed files with 1676 additions and 1401 deletions

159
src/core/util/error.hpp Normal file
View file

@ -0,0 +1,159 @@
#pragma once
#include <format> // std::format
#include <source_location> // std::source_location
#include <string_view> // std::string_view (StringView)
#include <system_error> // std::error_code
#ifdef _WIN32
#include <winrt/base.h> // winrt::hresult_error
#elifdef __linux__
#include <dbus-cxx/error.h> // DBus::Error
#endif
#include "src/core/util/types.hpp"
namespace util::error {
using types::u8, types::i32, types::String, types::StringView, types::Exception;
/**
* @enum DraconisErrorCode
* @brief Error codes for general OS-level operations.
*/
enum class DraconisErrorCode : u8 {
IoError, ///< General I/O error (filesystem, pipes, etc.).
PermissionDenied, ///< Insufficient permissions to perform the operation.
NotFound, ///< A required resource (file, registry key, device, API endpoint) was not found.
ParseError, ///< Failed to parse data obtained from the OS (e.g., file content, API output).
ApiUnavailable, ///< A required OS service/API is unavailable or failed unexpectedly at runtime.
NotSupported, ///< The requested operation is not supported on this platform, version, or configuration.
Timeout, ///< An operation timed out (e.g., waiting for IPC reply).
BufferTooSmall, ///< Optional: Keep if using fixed C-style buffers, otherwise remove.
InternalError, ///< An error occurred within the application's OS abstraction code logic.
NetworkError, ///< A network-related error occurred (e.g., DNS resolution, connection failure).
PlatformSpecific, ///< An unmapped error specific to the underlying OS platform occurred (check message).
Other, ///< A generic or unclassified error originating from the OS or an external library.
};
/**
* @struct DraconisError
* @brief Holds structured information about an OS-level error.
*
* Used as the error type in Result for many os:: functions.
*/
struct DraconisError {
// ReSharper disable CppDFANotInitializedField
String message; ///< A descriptive error message, potentially including platform details.
DraconisErrorCode code; ///< The general category of the error.
std::source_location location; ///< The source location where the error occurred (file, line, function).
// ReSharper restore CppDFANotInitializedField
DraconisError(
const DraconisErrorCode errc,
String msg,
const std::source_location& loc = std::source_location::current()
)
: message(std::move(msg)), code(errc), location(loc) {}
explicit DraconisError(const Exception& exc, const std::source_location& loc = std::source_location::current())
: message(exc.what()), code(DraconisErrorCode::InternalError), location(loc) {}
explicit DraconisError(
const std::error_code& errc,
const std::source_location& loc = std::source_location::current()
)
: message(errc.message()), location(loc) {
using enum DraconisErrorCode;
using enum std::errc;
switch (static_cast<std::errc>(errc.value())) {
case permission_denied: code = PermissionDenied; break;
case no_such_file_or_directory: code = NotFound; break;
case timed_out: code = Timeout; break;
case io_error: code = IoError; break;
case network_unreachable:
case network_down:
case connection_refused: code = NetworkError; break;
case not_supported: code = NotSupported; break;
default: code = errc.category() == std::generic_category() ? InternalError : PlatformSpecific; break;
}
}
#ifdef _WIN32
explicit OsError(const winrt::hresult_error& e) : message(winrt::to_string(e.message())) {
switch (e.code()) {
case E_ACCESSDENIED: code = OsErrorCode::PermissionDenied; break;
case HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND):
case HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_FOUND): code = OsErrorCode::NotFound; break;
case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
case HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT): code = OsErrorCode::Timeout; break;
case HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED): code = OsErrorCode::NotSupported; break;
default: code = OsErrorCode::PlatformSpecific; break;
}
}
#else
DraconisError(const DraconisErrorCode code_hint, const int errno_val)
: message(std::system_category().message(errno_val)), code(code_hint) {
using enum DraconisErrorCode;
switch (errno_val) {
case EACCES: code = PermissionDenied; break;
case ENOENT: code = NotFound; break;
case ETIMEDOUT: code = Timeout; break;
case ENOTSUP: code = NotSupported; break;
default: code = PlatformSpecific; break;
}
}
static auto withErrno(const String& context, const std::source_location& loc = std::source_location::current())
-> DraconisError {
const i32 errNo = errno;
const String msg = std::system_category().message(errNo);
const String fullMsg = std::format("{}: {}", context, msg);
DraconisErrorCode code = DraconisErrorCode::PlatformSpecific;
switch (errNo) {
case EACCES:
case EPERM: code = DraconisErrorCode::PermissionDenied; break;
case ENOENT: code = DraconisErrorCode::NotFound; break;
case ETIMEDOUT: code = DraconisErrorCode::Timeout; break;
case ENOTSUP: code = DraconisErrorCode::NotSupported; break;
case EIO: code = DraconisErrorCode::IoError; break;
case ECONNREFUSED:
case ENETDOWN:
case ENETUNREACH: code = DraconisErrorCode::NetworkError; break;
default: code = DraconisErrorCode::PlatformSpecific; break;
}
return DraconisError { code, fullMsg, loc };
}
#ifdef __linux__
static auto fromDBus(const DBus::Error& err, const std::source_location& loc = std::source_location::current())
-> DraconisError {
String name = err.name();
DraconisErrorCode codeHint = DraconisErrorCode::PlatformSpecific;
String message;
using namespace std::string_view_literals;
if (name == "org.freedesktop.DBus.Error.ServiceUnknown"sv ||
name == "org.freedesktop.DBus.Error.NameHasNoOwner"sv) {
codeHint = DraconisErrorCode::NotFound;
message = std::format("DBus service/name not found: {}", err.message());
} else if (name == "org.freedesktop.DBus.Error.NoReply"sv || name == "org.freedesktop.DBus.Error.Timeout"sv) {
codeHint = DraconisErrorCode::Timeout;
message = std::format("DBus timeout/no reply: {}", err.message());
} else if (name == "org.freedesktop.DBus.Error.AccessDenied"sv) {
codeHint = DraconisErrorCode::PermissionDenied;
message = std::format("DBus access denied: {}", err.message());
} else {
message = std::format("DBus error: {} - {}", name, err.message());
}
return DraconisError { codeHint, message, loc };
}
#endif
#endif
};
} // namespace util::error