9#ifndef RAD_EXCEPTIONS_HPP
10#define RAD_EXCEPTIONS_HPP
15#include <boost/stacktrace.hpp>
16#include <boost/filesystem.hpp>
17#include <boost/core/demangle.hpp>
66 typedef std::chrono::system_clock
ClockT;
73 explicit Exception(
const std::string& message);
75 explicit Exception(
const std::string& message,
const std::string& info);
242 std::string
Dump()
const;
256 virtual const char*
what() const noexcept override;
265 const
std::
string AdditionalDetails() const;
271 boost::stacktrace::stacktrace stack_trace;
272 std::chrono::time_point<ClockT> date_time;
273 std::thread::id thread_id;
274 uint32_t line_number;
275 std::string function_name;
276 std::string file_name;
278 bool detail_set_by_user;
279 std::string class_name;
281 std::vector<std::string> exception_stack;
284 std::shared_ptr<Attrs> m_attrs;
291T CreateExceptionWithMessage(
const char* file,
int line,
const char* function,
292 const std::string& msg) {
293 std::string fullmsg = msg;
294 fullmsg +=
"\nSource file: " + std::string(file);
295 fullmsg +=
"\nLine no.: " + std::to_string(line);
296 fullmsg +=
"\nFunction: " + std::string(function);
300template <
typename T,
typename N>
301T CreateExceptionObject(
const N& nested_exception,
const char* file,
int line,
302 const char* function,
const std::string& msg) {
303 if constexpr (std::is_base_of_v<Exception, T>) {
304 T throwing_exception = T(msg);
305 throwing_exception.SetFileName(boost::filesystem::path(file).filename().
string());
306 throwing_exception.SetFunctionName(function);
307 throwing_exception.SetLineNumber(line);
308 throwing_exception.SetClassName(boost::core::demangle(
typeid(T).name()));
309 std::vector<std::string> this_stack;
310 this_stack.push_back(throwing_exception.GetTypeName());
311 if constexpr (std::is_base_of_v<Exception, N>) {
313 std::vector<std::string> nested_stack = nested_exception.GetExceptionStack();
314 this_stack.insert(this_stack.end(), nested_stack.begin(), nested_stack.end());
317 throwing_exception.SetExceptionStack(this_stack);
318 return throwing_exception;
319 }
else if constexpr (std::is_base_of_v<std::runtime_error, T> or
320 std::is_base_of_v<std::logic_error, T>) {
321 return CreateExceptionWithMessage<T>(file, line, function, msg);
334#define RAD_THROW(exceptionType_t, msg) { \
335 throw rad::CreateExceptionObject<exceptionType_t>(0, __FILE__, __LINE__, __FUNCTION__, \
345#define RAD_RETHROW(exceptionType_t, nested_exception, msg) { \
346 exceptionType_t throwing_exception = rad::CreateExceptionObject<exceptionType_t>( \
347 nested_exception, __FILE__, __LINE__, __FUNCTION__, msg); \
348 std::throw_with_nested(throwing_exception); \
356#define RAD_LOG_THROW(exceptionType_t, logger, msg) { \
357 LOG4CPLUS_ERROR(logger, msg); \
358 throw rad::CreateExceptionObject<exceptionType_t>(0, __FILE__, __LINE__, __FUNCTION__, \
Base class for the exceptions thrown by RAD and its users.
Definition exceptions.hpp:53
int GetProcessId() const
Return the id of the process in which the exception was created.
Definition exceptions.cpp:111
const std::string & GetFileName() const
Return the name of the file in which the exception was thrown.
Definition exceptions.cpp:153
void SetLineNumber(uint32_t line_number)
Set the line number in which the exception was thrown.
Definition exceptions.cpp:123
std::string GetDetails() const
Get the exception details.
Definition exceptions.cpp:162
void SetDetails(const std::string &details)
Set the exception details.
Definition exceptions.cpp:157
Exception(const std::string &message)
Constructor.
Definition exceptions.cpp:25
std::vector< std::string > GetExceptionStack() const
Retrieves the nested exception stack as string.
Definition exceptions.cpp:176
uint32_t GetLineNumber() const
Return the line number in which the exception was thrown.
Definition exceptions.cpp:137
void SetFileName(const std::string &file_name)
Set the name of the file in which the exception was thrown.
Definition exceptions.cpp:149
const boost::stacktrace::stacktrace & GetOriginalStackTrace() const
Return the stack trace captured in the most nested exception constructor.
Definition exceptions.cpp:49
std::thread::id GetThreadId() const
Return the id of the thread in which the exception was created.
Definition exceptions.cpp:107
std::string GetStackTraceAsString() const
Same as GetStackTrace() but convert the trace to a string.
Definition exceptions.cpp:64
std::string GetFullStackTraceAsString() const
Return the full stack trace (including nested exceptions)
Definition exceptions.cpp:76
std::string Dump() const
Return the exception's details.
Definition exceptions.cpp:180
std::string GetOriginalStackTraceAsString() const
Same as GetOriginalStackTrace() but convert the trace to a string.
Definition exceptions.cpp:70
std::string GetTypeName() const
Return the exception class name.
Definition exceptions.cpp:119
void SetExceptionStack(const std::vector< std::string > &nested_exceptions)
Set the nested exceptions as strings.
Definition exceptions.cpp:172
void SetClassName(const std::string &class_name)
Set the name of the class in which the exception was thrown.
Definition exceptions.cpp:168
std::string GetHostName() const
Return the name of the host in which the exception was created.
Definition exceptions.cpp:115
int64_t GetCreationDate() const
Return the exception creation time as number of seconds since Unix Epoch.
Definition exceptions.cpp:101
virtual const char * what() const noexcept override
Return the exception message.
Definition exceptions.cpp:221
const std::string & GetFunctionName() const
Return the name of the function in which the exception was thrown.
Definition exceptions.cpp:145
std::chrono::system_clock ClockT
The clock to use for determining the exception creation time.
Definition exceptions.hpp:66
virtual ~Exception()=default
Destructor.
std::string DumpWithNested() const
Return all the exception's details including details of nested exceptions.
Definition exceptions.cpp:201
std::chrono::time_point< ClockT > GetTimePoint() const
Return the exception creation time as a chrono timepoint.
Definition exceptions.cpp:97
const boost::stacktrace::stacktrace & GetStackTrace() const
Return the stack trace captured in the exception constructor.
Definition exceptions.cpp:45
void SetFunctionName(const std::string &function_name)
Set the name of the function in which the exception was thrown.
Definition exceptions.cpp:141
Helper class header file.
Definition actionsApp.cpp:23
std::ostream & operator<<(std::ostream &os, const rad::Exception &e)
Definition exceptions.cpp:225