aboutsummaryrefslogtreecommitdiff
path: root/zencore/except.cpp
blob: 9bd4473083ab0c8bf53ea159574d50a83003e4d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright Epic Games, Inc. All Rights Reserved.

#include <fmt/format.h>
#include <zencore/except.h>
#include <system_error>

namespace zen {

void
ThrowSystemException([[maybe_unused]] HRESULT hRes, [[maybe_unused]] std::string_view Message)
{
	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);
	}
}

void
ThrowLastError(std::string_view Message)
{
	throw std::system_error(std::error_code(::GetLastError(), std::system_category()), std::string(Message));
}

std::string
GetLastErrorAsString()
{
	return GetWindowsErrorAsString(::GetLastError());
}

std::string
GetWindowsErrorAsString(uint32_t Win32ErrorCode)
{
	return std::error_code(Win32ErrorCode, std::system_category()).message();
}

void
ThrowLastError(std::string_view Message, const std::source_location& Location)
{
	using namespace fmt::literals;
	throw std::system_error(std::error_code(::GetLastError(), std::system_category()),
							"{}({}): {}"_format(Location.file_name(), Location.line(), Message));
}

}  // namespace zen