| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_SYSTEM_ERROR_SYSTEM_ERROR_HPP |
| 4 |
#define MPT_SYSTEM_ERROR_SYSTEM_ERROR_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/format/message.hpp" |
| 11 |
#include "mpt/format/message_macros.hpp" |
| 12 |
#include "mpt/format/simple.hpp" |
| 13 |
#include "mpt/string/types.hpp" |
| 14 |
#include "mpt/string_transcode/transcode.hpp" |
| 15 |
#include "mpt/out_of_memory/out_of_memory.hpp" |
| 16 |
|
| 17 |
#if MPT_OS_WINDOWS |
| 18 |
#include <stdexcept> |
| 19 |
#if MPT_OS_WINDOWS_WINRT |
| 20 |
#include <vector> |
| 21 |
#endif // MPT_OS_WINDOWS_WINRT |
| 22 |
#endif // MPT_OS_WINDOWS |
| 23 |
|
| 24 |
#if MPT_OS_WINDOWS |
| 25 |
#include <windows.h> |
| 26 |
#endif // MPT_OS_WINDOWS |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
namespace mpt { |
| 31 |
inline namespace MPT_INLINE_NS { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#if MPT_OS_WINDOWS |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
namespace windows { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
inline mpt::ustring GetErrorMessage(DWORD errorCode, HANDLE hModule = NULL) { |
| 44 |
#if MPT_OS_WINDOWS_WINRT |
| 45 |
std::vector<TCHAR> msgbuf(65536); |
| 46 |
if (FormatMessage( |
| 47 |
(hModule ? FORMAT_MESSAGE_FROM_HMODULE : 0) | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 48 |
hModule, |
| 49 |
errorCode, |
| 50 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 51 |
msgbuf.data(), |
| 52 |
mpt::saturate_cast<DWORD>(msgbuf.size()), |
| 53 |
NULL) |
| 54 |
== 0) |
| 55 |
{ |
| 56 |
DWORD e = GetLastError(); |
| 57 |
if ((e == ERROR_NOT_ENOUGH_MEMORY) || (e == ERROR_OUTOFMEMORY)) { |
| 58 |
mpt::throw_out_of_memory(); |
| 59 |
} |
| 60 |
return {}; |
| 61 |
} |
| 62 |
return mpt::transcode<mpt::ustring>(mpt::winstring{msgbuf.data()}); |
| 63 |
#else |
| 64 |
mpt::ustring message; |
| 65 |
void * lpMsgBuf = nullptr; |
| 66 |
if (FormatMessage( |
| 67 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | (hModule ? FORMAT_MESSAGE_FROM_HMODULE : 0) | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 68 |
hModule, |
| 69 |
errorCode, |
| 70 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 71 |
(LPTSTR)&lpMsgBuf, |
| 72 |
0, |
| 73 |
NULL) |
| 74 |
== 0) |
| 75 |
{ |
| 76 |
DWORD e = GetLastError(); |
| 77 |
if (lpMsgBuf) { |
| 78 |
LocalFree(lpMsgBuf); |
| 79 |
} |
| 80 |
if ((e == ERROR_NOT_ENOUGH_MEMORY) || (e == ERROR_OUTOFMEMORY)) { |
| 81 |
mpt::throw_out_of_memory(); |
| 82 |
} |
| 83 |
return {}; |
| 84 |
} |
| 85 |
if (!lpMsgBuf) { |
| 86 |
return {}; |
| 87 |
} |
| 88 |
try { |
| 89 |
message = mpt::transcode<mpt::ustring>(mpt::winstring{static_cast<LPTSTR>(lpMsgBuf)}); |
| 90 |
} catch (mpt::out_of_memory e) { |
| 91 |
LocalFree(lpMsgBuf); |
| 92 |
mpt::rethrow_out_of_memory(e); |
| 93 |
} |
| 94 |
LocalFree(lpMsgBuf); |
| 95 |
return message; |
| 96 |
#endif |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
class error |
| 101 |
: public std::runtime_error { |
| 102 |
public: |
| 103 |
error(DWORD errorCode, HANDLE hModule = NULL) |
| 104 |
: std::runtime_error(mpt::transcode<std::string>(mpt::exception_encoding, MPT_UFORMAT_MESSAGE("Windows Error: 0x{}: {}")(mpt::format<mpt::ustring>::hex0<8>(errorCode), GetErrorMessage(errorCode, hModule)))) { |
| 105 |
return; |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
inline HANDLE CheckFileHANDLE(HANDLE handle) { |
| 111 |
if (handle == INVALID_HANDLE_VALUE) { |
| 112 |
DWORD err = ::GetLastError(); |
| 113 |
if ((err == ERROR_NOT_ENOUGH_MEMORY) || (err == ERROR_OUTOFMEMORY)) { |
| 114 |
mpt::throw_out_of_memory(); |
| 115 |
} |
| 116 |
throw windows::error(err); |
| 117 |
} |
| 118 |
return handle; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
inline HANDLE CheckHANDLE(HANDLE handle) { |
| 123 |
if (handle == NULL) { |
| 124 |
DWORD err = ::GetLastError(); |
| 125 |
if ((err == ERROR_NOT_ENOUGH_MEMORY) || (err == ERROR_OUTOFMEMORY)) { |
| 126 |
mpt::throw_out_of_memory(); |
| 127 |
} |
| 128 |
throw windows::error(err); |
| 129 |
} |
| 130 |
return handle; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
inline void CheckBOOL(BOOL result) { |
| 135 |
if (result == FALSE) { |
| 136 |
DWORD err = ::GetLastError(); |
| 137 |
if ((err == ERROR_NOT_ENOUGH_MEMORY) || (err == ERROR_OUTOFMEMORY)) { |
| 138 |
mpt::throw_out_of_memory(); |
| 139 |
} |
| 140 |
throw windows::error(err); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
inline void ExpectError(DWORD expected) { |
| 146 |
DWORD err = ::GetLastError(); |
| 147 |
if (err != expected) { |
| 148 |
if ((err == ERROR_NOT_ENOUGH_MEMORY) || (err == ERROR_OUTOFMEMORY)) { |
| 149 |
mpt::throw_out_of_memory(); |
| 150 |
} |
| 151 |
throw windows::error(err); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
} // namespace windows |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
#endif // MPT_OS_WINDOWS |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
} // namespace MPT_INLINE_NS |
| 166 |
} // namespace mpt |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
#endif // MPT_SYSTEM_ERROR_SYSTEM_ERROR_HPP |