forked from LadybirdBrowser/ladybird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.cpp
52 lines (44 loc) · 1.38 KB
/
Error.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
/*
* Copyright (c) 2023, Liav A. <[email protected]>
* Copyright (c) 2023, Cameron Youell <[email protected]>
* Copyright (c) 2024, stasoid <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Error.h>
#ifdef AK_OS_WINDOWS
# include <AK/ByteString.h>
# include <AK/HashMap.h>
# include <windows.h>
#endif
namespace AK {
Error Error::from_string_view_or_print_error_and_return_errno(StringView string_literal, [[maybe_unused]] int code)
{
return Error::from_string_view(string_literal);
}
#ifdef AK_OS_WINDOWS
Error Error::from_windows_error(DWORD code)
{
static HashMap<DWORD, ByteString> windows_errors;
auto string = windows_errors.get(code);
if (string.has_value()) {
return Error::from_string_view(string->view());
} else {
char* message = nullptr;
auto size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&message,
0,
nullptr);
if (size == 0)
return Error::from_string_view_or_print_error_and_return_errno("Unknown error"sv, code);
windows_errors.set(code, { message, size });
LocalFree(message);
return from_windows_error(code);
}
}
#endif
}