Go to the documentation of this file.
9 #ifndef RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
10 #define RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
24 #include <type_traits>
26 #include <boost/stacktrace.hpp>
27 #include <boost/filesystem.hpp>
28 #include <boost/core/demangle.hpp>
49 typedef std::chrono::system_clock
clock_t;
65 template<
typename ...
Args>
67 :
CiiException(BuildFormattedString(formatter, args...)) {
76 return m_attribs->ciiType;
80 return m_attribs->ciiMethodName;
84 return m_attribs->ciiClassName;
88 return m_attribs->ciiFileName;
92 return m_attribs->ciiHostName;
98 return m_attribs->ciiExceptionStack;
102 return m_attribs->ciiMessage;
106 return m_attribs->ciiDetails;
110 return m_attribs->ciiCreationDate;
263 std::string
Dump()
const;
275 const char*
what() const noexcept override;
283 const
char*
What() const noexcept {
293 template<
typename ...
Args>
294 static std::string BuildFormattedString(
const std::string& formatter,
296 int finalStringSize = std::snprintf(
nullptr, 0, formatter.c_str(), args...);
297 std::vector<char> buffer((
size_t) finalStringSize + 1);
298 std::snprintf(buffer.data(), buffer.size(), formatter.c_str(), args...);
299 return std::string(buffer.data(), (
size_t) finalStringSize);
308 const std::string AdditionalDetails()
const;
314 std::string ciiMethodName;
315 std::string ciiClassName;
316 std::string ciiFileName;
317 std::string ciiHostName;
318 std::string ciiStackTrace;
319 std::vector<std::string> ciiExceptionStack;
320 std::string ciiMessage;
321 std::string ciiDetails;
322 int64_t ciiCreationDate;
324 const boost::stacktrace::stacktrace m_stack_trace;
325 const std::chrono::time_point<clock_t> m_date_time;
326 const std::thread::id m_thread_id;
327 const int m_process_id;
328 const std::string m_host_name;
331 std::string m_function_name;
332 std::string m_file_name;
333 std::string m_details;
334 std::string m_severity;
335 bool m_detail_set_by_user;
338 std::shared_ptr<Attributes> m_attribs;
377 template <
typename T,
typename...
Args>
378 T CreateExceptionWithMessage(
const char* file,
int line,
const char*
function,
379 const std::string& msg,
Args&&... args) {
380 std::string fullmsg = msg;
381 fullmsg +=
"\nSource file: " + std::string(file);
382 fullmsg +=
"\nLine no.: " + std::to_string(line);
383 fullmsg +=
"\nFunction: " + std::string(
function);
384 return T(fullmsg, std::forward<Args>(args)...);
390 template <
typename T,
typename N,
typename...
Args>
391 T CreateExceptionObject(
const N& nested_exception,
const char* file,
int line,
const char*
function,
393 if constexpr (std::is_base_of_v<elt::error::CiiException, T>) {
394 T throwing_exception = T(std::forward<Args>(args)...);
396 throwing_exception.SetFunctionName(
function);
397 throwing_exception.SetLineNumber(line);
398 throwing_exception.SetClassName(boost::core::demangle(
typeid(T).name()));
399 std::vector<std::string> this_stack;
400 this_stack.push_back(throwing_exception.GetTypeName());
401 if constexpr (std::is_base_of_v<elt::error::CiiException, N>) {
402 std::vector<std::string> nested_stack = nested_exception.getCiiExceptionStack();
403 this_stack.insert(this_stack.end(), nested_stack.begin(), nested_stack.end());
405 throwing_exception.SetCiiExceptionStack(this_stack);
406 return throwing_exception;
407 }
else if constexpr (std::is_base_of_v<std::runtime_error, T> or
408 std::is_base_of_v<std::logic_error, T>) {
409 return CreateExceptionWithMessage<T>(file, line,
function, std::forward<Args>(args)...);
411 return T(std::forward<Args>(args)...);
422 #define CII_THROW(exceptionType_t, ...) { \
423 throw CreateExceptionObject<exceptionType_t>(0, __FILE__, __LINE__, __FUNCTION__, \
433 #define CII_THROW_WITH_NESTED(exceptionType_t, nested_exception, ...) { \
434 exceptionType_t throwing_exception = CreateExceptionObject<exceptionType_t>( \
435 nested_exception, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); \
436 std::throw_with_nested(throwing_exception); \
439 #endif // RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
const std::string & GetFileName() const
Return the name of the file in which the exception was thrown.
Definition: exceptions.cpp:158
std::chrono::time_point< clock_t > GetTimePoint() const
Return the exception creation time as a chrono timepoint.
Definition: exceptions.cpp:101
std::string DumpWithNested() const
Return all the exception's details, including details of nested exceptions.
Definition: exceptions.cpp:200
std::string GetSeverity() const
Definition: exceptions.hpp:362
std::ostream & operator<<(std::ostream &os, const elt::error::CiiException &e)
Definition: exceptions.cpp:233
virtual std::string getCiiMethodName() const
Definition: exceptions.hpp:79
const char * What() const noexcept
Return all the exception's details, including details of nested exceptions.
Definition: exceptions.hpp:283
Base class for the exceptions.
Definition: exceptions.hpp:345
detail::Args Args
Definition: rtcComponent.hpp:31
virtual std::string getCiiFileName() const
Definition: exceptions.hpp:87
std::string GetFullStackTraceAsString() const
Return the full stack trace (including nested exceptions)
Definition: exceptions.cpp:80
std::string GetDetails() const
Get the exception details.
Definition: exceptions.cpp:168
std::string GetSeverity() const
Definition: exceptions.hpp:349
string path
Definition: rtctkExampleDataTaskRobotTest.py:228
int64_t GetCreationDate() const
Return the exception creation time as number of seconds since Unix Epoch.
Definition: exceptions.cpp:105
Base class for errors.
Definition: exceptions.hpp:358
virtual std::string getCiiMessage() const
Definition: exceptions.hpp:101
const boost::stacktrace::stacktrace & GetOriginalStackTrace() const
Return the stack trace captured in the most nested exception constructor.
Definition: exceptions.cpp:46
const std::string & GetHostName() const
Return the name of the host in which the exception was created.
Definition: exceptions.cpp:118
void SetFileName(const std::string &fileName)
Set the name of the file in which the exception was thrown.
Definition: exceptions.cpp:153
int GetProcessId() const
Return the id of the process in which the exception was created.
Definition: exceptions.cpp:114
Base class for the exceptions thrown by the client API.
Definition: exceptions.hpp:36
std::string GetStackTraceAsString() const
Same as GetStackTrace() but convert the trace to a string.
Definition: exceptions.cpp:68
virtual std::string getCiiHostName() const
Definition: exceptions.hpp:91
std::string GetOriginalStackTraceAsString() const
Same as GetOriginalStackTrace() but convert the trace to a string.
Definition: exceptions.cpp:74
const boost::stacktrace::stacktrace & GetStackTrace() const
Return the stack trace captured in the exception constructor.
Definition: exceptions.cpp:42
virtual std::string getCiiClassName() const
Definition: exceptions.hpp:83
Definition: exceptions.cpp:21
CiiException(const std::string &message)
Constructor.
Definition: exceptions.cpp:34
virtual ~CiiException()=default
Destructor.
const char * what() const noexcept override
Definition: exceptions.cpp:226
std::string GetSeverity() const
std::string GetTypeName() const
Return the exception class name.
Definition: exceptions.cpp:122
virtual std::vector< std::string > getCiiExceptionStack() const
Definition: exceptions.hpp:97
unsigned int uint32_t
Definition: mudpi.h:16
void SetClassName(const std::string &className)
Set the name of the class in which the exception was thrown.
Definition: exceptions.cpp:174
void SetCiiExceptionStack(const std::vector< std::string > &nested_exceptions)
Definition: exceptions.cpp:221
const std::string GetCiiStackTrace() const noexcept
Definition: exceptions.hpp:113
const std::string & GetFunctionName() const
Return the name of the function in which the exception was thrown.
Definition: exceptions.cpp:149
std::thread::id GetThreadId() const
Return the id of the thread in which the exception was created.
Definition: exceptions.cpp:110
virtual int64_t getCiiCreationDate() const
Definition: exceptions.hpp:109
std::chrono::system_clock clock_t
The clock to use for determining the exception creation time.
Definition: exceptions.hpp:49
virtual std::string getCiiDetails() const
Definition: exceptions.hpp:105
virtual std::string getCiiType() const
Definition: exceptions.hpp:75
uint32_t GetLineNumber() const
Return the line number in which the exception was thrown.
Definition: exceptions.cpp:140
void error(const char *msg)
Definition: main.cpp:29
virtual std::string getCiiStackTrace() const
Definition: exceptions.cpp:61
void SetDetails(const std::string &details)
Set the exception details.
Definition: exceptions.cpp:162
filename
Definition: rtctkExampleDataTaskGenFitsData.py:13
std::string Dump() const
Return all the exception's details.
Definition: exceptions.cpp:178
void SetFunctionName(const std::string &functionName)
Set the name of the function in which the exception was thrown.
Definition: exceptions.cpp:144
void SetLineNumber(uint32_t lineNumber)
Set the line number in which the exception was thrown.
Definition: exceptions.cpp:126