aboutsummaryrefslogtreecommitdiff
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
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
-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
-rw-r--r--zenserver/experimental/usnjournal.cpp5
-rw-r--r--zenstore/caslog.cpp9
6 files changed, 34 insertions, 28 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);
diff --git a/zenserver/experimental/usnjournal.cpp b/zenserver/experimental/usnjournal.cpp
index f44e50945..a266c2338 100644
--- a/zenserver/experimental/usnjournal.cpp
+++ b/zenserver/experimental/usnjournal.cpp
@@ -164,7 +164,10 @@ UsnJournalReader::Initialize(std::filesystem::path VolumePath)
HRESULT hRes =
VolumeRootDir.Create(VolumePathName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS);
- ThrowIfFailed(hRes, "Failed to open handle to volume root");
+ if (FAILED(hRes))
+ {
+ ThrowSystemException(hRes, "Failed to open handle to volume root");
+ }
FILE_ID_INFO FileInformation{};
BOOL Success = GetFileInformationByHandleEx(VolumeRootDir, FileIdInfo, &FileInformation, sizeof FileInformation);
diff --git a/zenstore/caslog.cpp b/zenstore/caslog.cpp
index 4b9aacb42..0ef3ed1bd 100644
--- a/zenstore/caslog.cpp
+++ b/zenstore/caslog.cpp
@@ -125,7 +125,10 @@ CasLogFile::Replay(std::function<void(const void*)>&& Handler)
m_File.Seek(LogBaseOffset, FILE_BEGIN);
HRESULT hRes = m_File.Read(ReadBuffer.data(), gsl::narrow<DWORD>(LogDataSize));
- zen::ThrowIfFailed(hRes, "Failed to read log file");
+ if (FAILED(hRes))
+ {
+ zen::ThrowSystemException(hRes, "Failed to read log file");
+ }
for (int i = 0; i < LogEntryCount; ++i)
{
@@ -140,9 +143,7 @@ CasLogFile::Append(const void* DataPointer, uint64_t DataSize)
if (FAILED(hRes))
{
- throw std::system_error(GetLastError(),
- std::system_category(),
- "Failed to write to log file '{}'"_format(zen::PathFromHandle(m_File)));
+ zen::ThrowSystemException(hRes, "Failed to write to log file '{}'"_format(zen::PathFromHandle(m_File)));
}
}