errors.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "crucible/windows/errors.hpp" #include "crucible/core/resource_warden.inl" #include "crucible/windows/transcoding.hpp" #include <Windows.h> #include <sstream> #include <type_traits> static_assert(sizeof(::DWORD) <= sizeof(std::uint32_t)); static_assert(std::is_unsigned_v<::DWORD> == std::is_unsigned_v<std::uint32_t>); namespace crucible::windows::errors { namespace { struct FormatWindowsError final { auto operator()(CreateDirectoryWFailed const &) const -> std::string { return "Call to host function CreateDirectoryW() failed"; } auto operator()(CreateFileWFailed const &) const -> std::string { return "Call to host function CreateFileW() failed"; } auto operator()(GetFileAttributesWFailed const &) const -> std::string { return "Call to host function GetFileAttributesW() failed"; } auto operator()(GetFileSizeExFailed const &) const -> std::string { return "Call to host function GetFileSizeEX() failed"; } auto operator()(FindFirstFileWFailed const &) const -> std::string { return "Call to host function FindFirstFileW() failed"; } auto operator()(FindNextFileWFailed const &) const -> std::string { return "Call to host function FindNextFileW() failed"; } auto operator()(MultiByteToWideCharFailed const &) const -> std::string { return "Call to host function MultiByteToWideChar() failed"; } auto operator()(ReadFileFailed const &) const -> std::string { return "Call to host function ReadFile() failed"; } auto operator()(RemoveDirectoryWFailed const &) const -> std::string { return "Call to host function RemoveDirectoryW() failed"; } auto operator()(DeleteFileWFailed const &) const -> std::string { return "Call to host function DeleteFileW() failed"; } auto operator()(WideCharToMultiByteFailed const &) const -> std::string { return "Call to host function WideCharToMultiByte() failed"; } auto operator()(WriteFileFailed const &) const -> std::string { return "Call to host function WriteFile() failed"; } auto operator()(InputTooLarge const &) const -> std::string { return "Input too large"; } auto operator()(NegativeFileSize const &) const -> std::string { return "Negative file size"; } auto operator()(OutputTruncated const &) const -> std::string { return "Output was truncated"; } }; struct SourceWindowsError final { auto operator()(CreateDirectoryWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(CreateFileWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(GetFileAttributesWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(GetFileSizeExFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(FindFirstFileWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(FindNextFileWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(MultiByteToWideCharFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(ReadFileFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(RemoveDirectoryWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(DeleteFileWFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(WideCharToMultiByteFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(WriteFileFailed const &e) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::cref<core::errors::AbstractError>(e.host_error); } auto operator()(InputTooLarge const &) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::nullopt; } auto operator()(NegativeFileSize const &) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::nullopt; } auto operator()(OutputTruncated const &) const -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::nullopt; } }; template<typename T> struct LocalFreeAdapter final { static_assert(std::is_pointer_v<T>); auto operator()(T pointer) const -> void { ::LocalFree(pointer); } }; template<typename T> using LocalAllocWarden = core::resource_warden::ResourceWarden<T, LocalFreeAdapter<T>>; auto get_host_error_code() -> std::uint32_t { ::DWORD const error_dword { ::GetLastError() }; return static_cast<std::uint32_t>(error_dword); } } HostError::HostError() : my_error_code { get_host_error_code() } { } HostError::~HostError() = default; auto HostError::format() const -> std::string { constexpr ::DWORD FLAGS { FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK }; constexpr ::DWORD LANGUAGE_ID { 0 }; constexpr ::DWORD MINIMUM_SIZE { 0 }; ::LPVOID message_source { nullptr }; auto const error_dword { static_cast<::DWORD>(my_error_code) }; ::LPWSTR raw_utf16_message { nullptr }; auto dont_blame_me_blame_microsoft { reinterpret_cast<::LPWSTR>(&raw_utf16_message) }; ::va_list *arguments { nullptr }; ::DWORD const characters_written { ::FormatMessageW(FLAGS, message_source, error_dword, LANGUAGE_ID, dont_blame_me_blame_microsoft, MINIMUM_SIZE, arguments) }; if (characters_written == 0) { ::DWORD const cause { ::GetLastError() }; std::ostringstream buffer {}; buffer << "Failed to format message for Windows API error code `" << my_error_code << "`" << " " << "(Error code returned from FormatMessageW() was: `" << cause << "`)"; return buffer.str(); } LocalAllocWarden<::LPWSTR> warden(raw_utf16_message); ::DWORD const characters_written_excluding_terminator { characters_written - 1 }; std::wstring const utf16_message(raw_utf16_message, characters_written_excluding_terminator); auto const transcoding_result { transcoding::transcode_utf16_to_utf8(utf16_message) }; if (transcoding_result.has_failure()) { std::ostringstream buffer {}; buffer << "Failed to transcode Windows API error message from UTF16 to UTF8 for error code `" << my_error_code << "`"; return buffer.str(); } return std::move(transcoding_result).get_success(); } auto HostError::source() const & -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::nullopt; } WindowsError::WindowsError(CreateDirectoryWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(CreateFileWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(GetFileAttributesWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(GetFileSizeExFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(FindFirstFileWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(FindNextFileWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(MultiByteToWideCharFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(ReadFileFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(RemoveDirectoryWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(DeleteFileWFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(WideCharToMultiByteFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(WriteFileFailed const &variant) : my_variant { variant } { } WindowsError::WindowsError(InputTooLarge const &variant) : my_variant { variant } { } WindowsError::WindowsError(NegativeFileSize const &variant) : my_variant { variant } { } WindowsError::WindowsError(OutputTruncated const &variant) : my_variant { variant } { } WindowsError::~WindowsError() = default; auto WindowsError::format() const -> std::string { return std::visit(FormatWindowsError {}, my_variant); } auto WindowsError::source() const & -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> { return std::visit(SourceWindowsError {}, my_variant); } }