aboutsummaryrefslogtreecommitdiff
path: root/zencore
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-05-21 21:17:12 +0200
committerStefan Boberg <[email protected]>2021-05-21 21:17:12 +0200
commit9f62b35a7380db253cce3310fa5208b8c8e20ef5 (patch)
treeea6bdb2a876649c2eb5e1142d9c1a2a2d1c96ca8 /zencore
parentRenamed CasBlobFile -> BasicFile (diff)
downloadzen-9f62b35a7380db253cce3310fa5208b8c8e20ef5.tar.xz
zen-9f62b35a7380db253cce3310fa5208b8c8e20ef5.zip
Cleaned up exception handling
We now use std::system_error where possible to report Win32 system errors. We still have WindowsException for general HRESULT based errors but we should phase it out where possible
Diffstat (limited to 'zencore')
-rw-r--r--zencore/except.cpp20
-rw-r--r--zencore/filesystem.cpp6
-rw-r--r--zencore/include/zencore/except.h18
-rw-r--r--zencore/thread.cpp4
4 files changed, 25 insertions, 23 deletions
diff --git a/zencore/except.cpp b/zencore/except.cpp
index b02122f58..882f69f9a 100644
--- a/zencore/except.cpp
+++ b/zencore/except.cpp
@@ -1,17 +1,27 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/except.h>
+#include <system_error>
namespace zen {
void
-ThrowSystemException([[maybe_unused]] HRESULT hRes, [[maybe_unused]] const char* Message)
+ThrowSystemException([[maybe_unused]] HRESULT hRes, [[maybe_unused]] std::string_view Message)
{
- // TODO
-
- int ErrValue = hRes;
+ if (HRESULT_FACILITY(hRes) == FACILITY_WIN32)
+ {
+ throw std::system_error(std::error_code(hRes & 0xffff, std::system_category()), std::string(Message));
+ }
+ else
+ {
+ throw WindowsException(hRes, Message);
+ }
+}
- throw std::system_error(ErrValue, std::system_category(), Message);
+void
+ThrowLastError(std::string_view Message)
+{
+ throw std::system_error(std::error_code(::GetLastError(), std::system_category()), std::string(Message));
}
} // namespace zen
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp
index 578b28277..cb806b276 100644
--- a/zencore/filesystem.cpp
+++ b/zencore/filesystem.cpp
@@ -409,7 +409,7 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
HRESULT hRes = Outfile.Create(Path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS);
if (FAILED(hRes))
{
- zen::ThrowIfFailed(hRes, "File open failed for '{}'"_format(Path).c_str());
+ zen::ThrowSystemException(hRes, "File open failed for '{}'"_format(Path).c_str());
}
// TODO: this could be block-enlightened
@@ -427,7 +427,7 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
if (FAILED(hRes))
{
- zen::ThrowIfFailed(hRes, "File write failed for '{}'"_format(Path).c_str());
+ zen::ThrowSystemException(hRes, "File write failed for '{}'"_format(Path).c_str());
}
WriteSize -= ChunkSize;
@@ -517,7 +517,7 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS);
- zen::ThrowIfFailed(hRes, "Failed to open handle to volume root");
+ zen::ThrowSystemException(hRes, "Failed to open handle to volume root");
while (Continue)
{
diff --git a/zencore/include/zencore/except.h b/zencore/include/zencore/except.h
index 8f5f50e86..782dbeed0 100644
--- a/zencore/include/zencore/except.h
+++ b/zencore/include/zencore/except.h
@@ -11,18 +11,12 @@ namespace zen {
class WindowsException : public std::exception
{
public:
- WindowsException(const char* Message)
+ WindowsException(std::string_view Message)
{
m_hResult = HRESULT_FROM_WIN32(GetLastError());
m_Message = Message;
}
- WindowsException(HRESULT hRes, const char* Message)
- {
- m_hResult = hRes;
- m_Message = Message;
- }
-
WindowsException(HRESULT hRes, std::string_view Message)
{
m_hResult = hRes;
@@ -49,18 +43,14 @@ private:
HRESULT m_hResult;
};
-ZENCORE_API void ThrowSystemException(HRESULT hRes, const char* Message);
+ZENCORE_API void ThrowSystemException(HRESULT hRes, std::string_view Message);
+
inline void
ThrowSystemException(const char* Message)
{
throw WindowsException(Message);
}
-inline void
-ThrowIfFailed(HRESULT hRes, const char* Message)
-{
- if (FAILED(hRes))
- ThrowSystemException(hRes, Message);
-}
+ZENCORE_API void ThrowLastError(std::string_view Message);
} // namespace zen
diff --git a/zencore/thread.cpp b/zencore/thread.cpp
index 80cf6f100..4451fd302 100644
--- a/zencore/thread.cpp
+++ b/zencore/thread.cpp
@@ -58,13 +58,15 @@ Event::Reset()
bool
Event::Wait(int TimeoutMs)
{
+ using namespace std::literals;
+
const DWORD Timeout = (TimeoutMs < 0) ? INFINITE : TimeoutMs;
DWORD Result = WaitForSingleObject(m_EventHandle, Timeout);
if (Result == WAIT_FAILED)
{
- throw WindowsException("Event wait failed");
+ zen::ThrowLastError("Event wait failed"sv);
}
return (Result == WAIT_OBJECT_0);