Skip to content

Commit de8c7b0

Browse files
committed
Log on internal error in addition to possibly rethrowing
1 parent d00bcb8 commit de8c7b0

File tree

4 files changed

+52
-54
lines changed

4 files changed

+52
-54
lines changed

src/cpptrace.cpp

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstdint>
66
#include <cstdio>
77
#include <cstring>
8+
#include <exception>
89
#include <iostream>
910
#include <sstream>
1011
#include <string>
@@ -39,9 +40,7 @@ CPPTRACE_BEGIN_NAMESPACE
3940
try { // try/catch can never be hit but it's needed to prevent TCO
4041
return generate_raw_trace(skip + 1);
4142
} catch(...) {
42-
if(!detail::should_absorb_trace_exceptions()) {
43-
throw;
44-
}
43+
detail::log_and_maybe_propagate_exception(std::current_exception());
4544
return raw_trace{};
4645
}
4746
}
@@ -51,9 +50,7 @@ CPPTRACE_BEGIN_NAMESPACE
5150
try { // try/catch can never be hit but it's needed to prevent TCO
5251
return generate_raw_trace(skip + 1, max_depth);
5352
} catch(...) {
54-
if(!detail::should_absorb_trace_exceptions()) {
55-
throw;
56-
}
53+
detail::log_and_maybe_propagate_exception(std::current_exception());
5754
return raw_trace{};
5855
}
5956
}
@@ -62,9 +59,7 @@ CPPTRACE_BEGIN_NAMESPACE
6259
try {
6360
return object_trace{detail::get_frames_object_info(frames)};
6461
} catch(...) { // NOSONAR
65-
if(!detail::should_absorb_trace_exceptions()) {
66-
throw;
67-
}
62+
detail::log_and_maybe_propagate_exception(std::current_exception());
6863
return object_trace{};
6964
}
7065
}
@@ -77,9 +72,7 @@ CPPTRACE_BEGIN_NAMESPACE
7772
}
7873
return {std::move(trace)};
7974
} catch(...) { // NOSONAR
80-
if(!detail::should_absorb_trace_exceptions()) {
81-
throw;
82-
}
75+
detail::log_and_maybe_propagate_exception(std::current_exception());
8376
return stacktrace{};
8477
}
8578
}
@@ -97,9 +90,7 @@ CPPTRACE_BEGIN_NAMESPACE
9790
try { // try/catch can never be hit but it's needed to prevent TCO
9891
return generate_object_trace(skip + 1);
9992
} catch(...) {
100-
if(!detail::should_absorb_trace_exceptions()) {
101-
throw;
102-
}
93+
detail::log_and_maybe_propagate_exception(std::current_exception());
10394
return object_trace{};
10495
}
10596
}
@@ -109,9 +100,7 @@ CPPTRACE_BEGIN_NAMESPACE
109100
try { // try/catch can never be hit but it's needed to prevent TCO
110101
return generate_object_trace(skip + 1, max_depth);
111102
} catch(...) {
112-
if(!detail::should_absorb_trace_exceptions()) {
113-
throw;
114-
}
103+
detail::log_and_maybe_propagate_exception(std::current_exception());
115104
return object_trace{};
116105
}
117106
}
@@ -124,9 +113,7 @@ CPPTRACE_BEGIN_NAMESPACE
124113
}
125114
return {std::move(trace)};
126115
} catch(...) { // NOSONAR
127-
if(!detail::should_absorb_trace_exceptions()) {
128-
throw;
129-
}
116+
detail::log_and_maybe_propagate_exception(std::current_exception());
130117
return stacktrace();
131118
}
132119
}
@@ -160,9 +147,7 @@ CPPTRACE_BEGIN_NAMESPACE
160147
try { // try/catch can never be hit but it's needed to prevent TCO
161148
return generate_trace(skip + 1);
162149
} catch(...) {
163-
if(!detail::should_absorb_trace_exceptions()) {
164-
throw;
165-
}
150+
detail::log_and_maybe_propagate_exception(std::current_exception());
166151
return stacktrace{};
167152
}
168153
}
@@ -172,9 +157,7 @@ CPPTRACE_BEGIN_NAMESPACE
172157
try { // try/catch can never be hit but it's needed to prevent TCO
173158
return generate_trace(skip + 1, max_depth);
174159
} catch(...) {
175-
if(!detail::should_absorb_trace_exceptions()) {
176-
throw;
177-
}
160+
detail::log_and_maybe_propagate_exception(std::current_exception());
178161
return stacktrace{};
179162
}
180163
}
@@ -225,9 +208,7 @@ CPPTRACE_BEGIN_NAMESPACE
225208
try {
226209
return raw_trace{detail::capture_frames(skip + 1, SIZE_MAX)};
227210
} catch(...) { // NOSONAR
228-
if(!detail::should_absorb_trace_exceptions()) {
229-
throw;
230-
}
211+
detail::log_and_maybe_propagate_exception(std::current_exception());
231212
return raw_trace{};
232213
}
233214
}
@@ -237,9 +218,7 @@ CPPTRACE_BEGIN_NAMESPACE
237218
try {
238219
return raw_trace{detail::capture_frames(skip + 1, max_depth)};
239220
} catch(...) { // NOSONAR
240-
if(!detail::should_absorb_trace_exceptions()) {
241-
throw;
242-
}
221+
detail::log_and_maybe_propagate_exception(std::current_exception());
243222
return raw_trace{};
244223
}
245224
}
@@ -249,9 +228,7 @@ CPPTRACE_BEGIN_NAMESPACE
249228
try { // try/catch can never be hit but it's needed to prevent TCO
250229
return detail::safe_capture_frames(buffer, size, skip + 1, SIZE_MAX);
251230
} catch(...) {
252-
if(!detail::should_absorb_trace_exceptions()) {
253-
throw;
254-
}
231+
detail::log_and_maybe_propagate_exception(std::current_exception());
255232
return 0;
256233
}
257234
}
@@ -266,9 +243,7 @@ CPPTRACE_BEGIN_NAMESPACE
266243
try { // try/catch can never be hit but it's needed to prevent TCO
267244
return detail::safe_capture_frames(buffer, size, skip + 1, max_depth);
268245
} catch(...) {
269-
if(!detail::should_absorb_trace_exceptions()) {
270-
throw;
271-
}
246+
detail::log_and_maybe_propagate_exception(std::current_exception());
272247
return 0;
273248
}
274249
}
@@ -278,9 +253,7 @@ CPPTRACE_BEGIN_NAMESPACE
278253
try {
279254
return object_trace{detail::get_frames_object_info(detail::capture_frames(skip + 1, SIZE_MAX))};
280255
} catch(...) { // NOSONAR
281-
if(!detail::should_absorb_trace_exceptions()) {
282-
throw;
283-
}
256+
detail::log_and_maybe_propagate_exception(std::current_exception());
284257
return object_trace{};
285258
}
286259
}
@@ -290,9 +263,7 @@ CPPTRACE_BEGIN_NAMESPACE
290263
try {
291264
return object_trace{detail::get_frames_object_info(detail::capture_frames(skip + 1, max_depth))};
292265
} catch(...) { // NOSONAR
293-
if(!detail::should_absorb_trace_exceptions()) {
294-
throw;
295-
}
266+
detail::log_and_maybe_propagate_exception(std::current_exception());
296267
return object_trace{};
297268
}
298269
}
@@ -302,9 +273,7 @@ CPPTRACE_BEGIN_NAMESPACE
302273
try { // try/catch can never be hit but it's needed to prevent TCO
303274
return generate_trace(skip + 1, SIZE_MAX);
304275
} catch(...) {
305-
if(!detail::should_absorb_trace_exceptions()) {
306-
throw;
307-
}
276+
detail::log_and_maybe_propagate_exception(std::current_exception());
308277
return stacktrace{};
309278
}
310279
}
@@ -319,9 +288,7 @@ CPPTRACE_BEGIN_NAMESPACE
319288
}
320289
return {std::move(trace)};
321290
} catch(...) { // NOSONAR
322-
if(!detail::should_absorb_trace_exceptions()) {
323-
throw;
324-
}
291+
detail::log_and_maybe_propagate_exception(std::current_exception());
325292
return stacktrace();
326293
}
327294
}

src/utils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ CPPTRACE_BEGIN_NAMESPACE
4141
try { // try/catch can never be hit but it's needed to prevent TCO
4242
detail::get_terminate_formatter().print(std::cerr, generate_trace(1));
4343
} catch(...) {
44-
if(!detail::should_absorb_trace_exceptions()) {
45-
throw;
46-
}
44+
detail::log_and_maybe_propagate_exception(std::current_exception());
4745
}
4846
}
4947

src/utils/error.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#include "utils/error.hpp"
22

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+
311
CPPTRACE_BEGIN_NAMESPACE
412
namespace detail {
513
internal_error::internal_error(std::string message) : msg("Cpptrace internal error: " + std::move(message)) {}
@@ -50,5 +58,28 @@ namespace detail {
5058
);
5159
}
5260
}
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+
}
5384
}
5485
CPPTRACE_END_NAMESPACE

src/utils/error.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ namespace detail {
125125
// Check condition in both debug. std::runtime_error on failure.
126126
#define ASSERT(...) PHONY_USE(__VA_ARGS__)
127127
#endif
128+
129+
void log_and_maybe_propagate_exception(std::exception_ptr);
128130
}
129131
CPPTRACE_END_NAMESPACE
130132

0 commit comments

Comments
 (0)