forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorHandling.cpp
71 lines (59 loc) · 2.13 KB
/
ErrorHandling.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/Support/ErrorHandling.h"
#include "llvh/ADT/Twine.h"
namespace hermes {
const std::error_category &oom_category() {
class OOMErrorCategory final : public std::error_category {
const char *name() const noexcept override {
return "vm_allocate_category";
}
std::string message(int condition) const override {
switch (static_cast<OOMError>(condition)) {
case OOMError::None:
return "No error";
case OOMError::MaxHeapReached:
return "Max heap size was exceeded";
case OOMError::MaxStorageReached:
return "Number of storages requested exceeded the limit";
case OOMError::Effective:
return "Effective OOM";
case OOMError::SuperSegmentAlloc:
return "Allocation occurred that was larger than a heap segment";
case OOMError::CopyableVectorCapacityIntegerOverflow:
return "CopyableVector capacity integer overflow";
case OOMError::TestVMLimitReached:
return "A test set a limit for virtual memory that was exceeded";
}
return "Unknown";
}
};
static OOMErrorCategory category;
return category;
}
std::error_code make_error_code(OOMError err) {
return std::error_code(static_cast<int>(err), oom_category());
}
std::string convert_error_to_message(std::error_code code) {
return (llvh::Twine("error_code(value = ") + llvh::Twine(code.value()) +
", category = " + code.category().name() +
", message = " + code.message() + ")")
.str();
}
LLVM_ATTRIBUTE_NORETURN void hermes_fatal(const char *msg) {
llvh::report_fatal_error(msg);
}
LLVM_ATTRIBUTE_NORETURN void hermes_fatal(const std::string &msg) {
llvh::report_fatal_error(msg.c_str());
}
LLVM_ATTRIBUTE_NORETURN void hermes_fatal(
llvh::StringRef prefix,
std::error_code code) {
llvh::report_fatal_error(
(llvh::Twine(prefix) + ": " + convert_error_to_message(code)).str());
}
} // namespace hermes