Echo Writes Code

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "crucible/windows/errors.hpp"

#include "crucible/core/resource_warden.inl"
#include "crucible/windows/transcoding.hpp"

#include <Windows.h>

#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 DescribeWindowsError final {
      auto operator()(CreateDirectoryWFailed const &e) const -> std::string {
        return "CreateDirectoryW() failed: " + e.host_error.describe();
      }

      auto operator()(CreateFileWFailed const &e) const -> std::string {
        return "CreateFileW() failed: " + e.host_error.describe();
      }

      auto operator()(GetFileAttributesWFailed const &e) const -> std::string {
        return "GetFileAttributesW() failed: " + e.host_error.describe();
      }

      auto operator()(GetFileSizeExFailed const &e) const -> std::string {
        return "GetFileSizeEx() failed: " + e.host_error.describe();
      }

      auto operator()(FindFirstFileWFailed const &e) const -> std::string {
        return "FindFirstFileW() failed: " + e.host_error.describe();
      }

      auto operator()(FindNextFileWFailed const &e) const -> std::string {
        return "FindNextFileW() failed: " + e.host_error.describe();
      }

      auto operator()(MultiByteToWideCharFailed const &e) const -> std::string {
        return "MultiByteToWideChar() failed: " + e.host_error.describe();
      }

      auto operator()(ReadFileFailed const &e) const -> std::string {
        return "ReadFile() failed: " + e.host_error.describe();
      }

      auto operator()(RemoveDirectoryWFailed const &e) const -> std::string {
        return "RemoveDirectoryW() failed: " + e.host_error.describe();
      }

      auto operator()(DeleteFileWFailed const &e) const -> std::string {
        return "DeleteFileW() failed: " + e.host_error.describe();
      }

      auto operator()(WideCharToMultiByteFailed const &e) const -> std::string {
        return "WideCharToMultiByte() failed: " + e.host_error.describe();
      }

      auto operator()(WriteFileFailed const &e) const -> std::string {
        return "WriteFile() failed: " + e.host_error.describe();
      }

      auto operator()(InputTooLarge const &) const -> std::string {
        return "Input was too large to transcode";
      }

      auto operator()(NegativeFileSize const &) const -> std::string {
        return "The system reported a negative file size";
      }

      auto operator()(OutputTruncated const &) const -> std::string {
        return "Output of transcoder was truncated";
      }
    };

    struct FormatWindowsError final {
      auto operator()(CreateDirectoryWFailed const &e) const -> std::string {
        return "CreateDirectoryWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(CreateFileWFailed const &e) const -> std::string {
        return "CreateFileWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(GetFileAttributesWFailed const &e) const -> std::string {
        return "GetFileAttributesWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(GetFileSizeExFailed const &e) const -> std::string {
        return "GetFileSizeExFailed { " + e.host_error.format() + " }";
      }

      auto operator()(FindFirstFileWFailed const &e) const -> std::string {
        return "FindFirstFileWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(FindNextFileWFailed const &e) const -> std::string {
        return "FindNextFileWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(MultiByteToWideCharFailed const &e) const -> std::string {
        return "MultiByteToWideCharFailed { " + e.host_error.format() + " }";
      }

      auto operator()(ReadFileFailed const &e) const -> std::string {
        return "ReadFileFailed { " + e.host_error.format() + " }";
      }

      auto operator()(RemoveDirectoryWFailed const &e) const -> std::string {
        return "RemoveDirectoryWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(DeleteFileWFailed const &e) const -> std::string {
        return "DeleteFileWFailed { " + e.host_error.format() + " }";
      }

      auto operator()(WideCharToMultiByteFailed const &e) const -> std::string {
        return "WideCharToMultiByteFailed { " + e.host_error.format() + " }";
      }

      auto operator()(WriteFileFailed const &e) const -> std::string {
        return "WriteFileFailed { " + e.host_error.format() + " }";
      }

      auto operator()(InputTooLarge const &) const -> std::string {
        return "InputTooLarge {}";
      }

      auto operator()(NegativeFileSize const &) const -> std::string {
        return "NegativeFileSize {}";
      }

      auto operator()(OutputTruncated const &) const -> std::string {
        return "OutputTruncated {}";
      }
    };

    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::describe() const -> std::string {
    return format_error_code();
  }

  auto HostError::format() const -> std::string {
    return "HostError { \"" + format_error_code() + "\" }";
  }

  auto HostError::source() const & -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> {
    return std::nullopt;
  }

  auto HostError::format_error_code() 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) {
      return "<failed to format message for Windows API error code `" + std::to_string(my_error_code) + "`>";
    }

    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_lhs()) {
      return "<failed to transcode message for Windows API error code `" + std::to_string(my_error_code) + "`>";
    }

    return transcoding_result.get_rhs();
  }

  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::describe() const -> std::string {
    return std::visit(DescribeWindowsError {}, my_variant);
  }

  auto WindowsError::format() const -> std::string {
    return "WindowsError { " + std::visit(FormatWindowsError {}, my_variant) + " }";
  }

  auto WindowsError::source() const & -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> {
    return std::visit(SourceWindowsError {}, my_variant);
  }
}