1717#include < Common/logger_useful.h>
1818
1919#include < algorithm>
20+ #include < atomic>
2021#include < cstdlib>
2122#include < cstring>
2223#include < filesystem>
@@ -37,6 +38,12 @@ namespace ErrorCodes
3738 extern const int LOGICAL_ERROR;
3839 extern const int CANNOT_ALLOCATE_MEMORY;
3940 extern const int CANNOT_MREMAP;
41+ extern const int POTENTIALLY_BROKEN_DATA_PART;
42+ extern const int REPLICA_ALREADY_EXISTS;
43+ extern const int NOT_ENOUGH_SPACE;
44+ extern const int CORRUPTED_DATA;
45+ extern const int CHECKSUM_DOESNT_MATCH;
46+ extern const int CANNOT_WRITE_TO_FILE_DESCRIPTOR;
4047}
4148
4249void abortOnFailedAssertion (const String & description, void * const * trace, size_t trace_offset, size_t trace_size)
@@ -263,6 +270,22 @@ void Exception::clearThreadFramePointers()
263270 thread_frame_pointers.frame_pointers .clear ();
264271}
265272
273+ Exception::~Exception ()
274+ {
275+ if (logged && logged->load (std::memory_order_relaxed))
276+ {
277+ const int error_code = code ();
278+ const bool is_error_important = error_code == ErrorCodes::LOGICAL_ERROR
279+ || error_code == ErrorCodes::POTENTIALLY_BROKEN_DATA_PART
280+ || error_code == ErrorCodes::REPLICA_ALREADY_EXISTS
281+ || error_code == ErrorCodes::NOT_ENOUGH_SPACE
282+ || error_code == ErrorCodes::CORRUPTED_DATA
283+ || error_code == ErrorCodes::CHECKSUM_DOESNT_MATCH
284+ || error_code == ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR;
285+ tryLogException (getLogger (" ~Exception" ), *this , " An important exception was likely ignored, here it is" );
286+ }
287+ }
288+
266289static void tryLogCurrentExceptionImpl (Poco::Logger * logger, const std::string & start_of_message, LogsLevel level)
267290{
268291 if (!isLoggingEnabled ())
@@ -290,6 +313,16 @@ static void tryLogCurrentExceptionImpl(Poco::Logger * logger, const std::string
290313 catch (...) // NOLINT(bugprone-empty-catch)
291314 {
292315 }
316+
317+ // / Mark the exception as logged.
318+ try
319+ {
320+ throw ;
321+ }
322+ catch (Exception & e)
323+ {
324+ e.markAsLogged ();
325+ }
293326}
294327
295328void tryLogCurrentException (const char * log_name, const std::string & start_of_message, LogsLevel level)
0 commit comments