Debian Codegraph @

src/utils/error.cpp

raw
1 #include "utils/error.hpp"
2
3 #include <cpptrace/utils.hpp>
4
5 #include <exception>
6
7 #include "platform/exception_type.hpp"
8 #include "logging.hpp"
9 #include "options.hpp"
10
11 CPPTRACE_BEGIN_NAMESPACE
12 namespace detail {
13 internal_error::internal_error(std::string message) : msg("Cpptrace internal error: " + std::move(message)) {}
14
15 constexpr const char* assert_actions[] = {"assertion", "verification", "panic"};
16 constexpr const char* assert_names[] = {"ASSERT", "VERIFY", "PANIC"};
17
18 void assert_fail(
19 assert_type type,
20 const char* expression,
21 const char* signature,
22 source_location location,
23 const char* message
24 ) {
25 const char* action = assert_actions[static_cast<std::underlying_type<assert_type>::type>(type)];
26 const char* name = assert_names[static_cast<std::underlying_type<assert_type>::type>(type)];
27 if(message == nullptr) {
28 throw internal_error(
29 "Cpptrace {} failed at {}:{}: {}\n"
30 " {}({});\n",
31 action, location.file, location.line, signature,
32 name, expression
33 );
34 } else {
35 throw internal_error(
36 "Cpptrace {} failed at {}:{}: {}: {}\n"
37 " {}({});\n",
38 action, location.file, location.line, signature, message,
39 name, expression
40 );
41 }
42 }
43
44 void panic(
45 const char* signature,
46 source_location location,
47 string_view message
48 ) {
49 if(message == "") {
50 throw internal_error(
51 "Cpptrace panic {}:{}: {}\n",
52 location.file, location.line, signature
53 );
54 } else {
55 throw internal_error(
56 "Cpptrace panic {}:{}: {}: {}\n",
57 location.file, location.line, signature, message
58 );
59 }
60 }
61
62 void log_and_maybe_propagate_exception(std::exception_ptr ptr) {
63 try {
64 if(ptr) {
65 std::rethrow_exception(ptr);
66 }
67 } catch(const internal_error& e) {
68 log::error("Unhandled cpptrace internal error: {}", e.what());
69 } catch(const std::exception& e) {
70 log::error(
71 "Unhandled cpptrace internal error of type {}: {}",
72 cpptrace::demangle(typeid(e).name()),
73 e.what()
74 );
75 } catch(...) {
76 log::error("Unhandled cpptrace internal error of type {}", detail::exception_type_name());
77 }
78 if(!should_absorb_trace_exceptions()) {
79 if(ptr) {
80 std::rethrow_exception(ptr);
81 }
82 }
83 }
84 }
85 CPPTRACE_END_NAMESPACE