Add new exception types: communication, protocol, and invalid argument
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 12 May 2022 18:27:06 +0000 (14:27 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 13 Jun 2022 20:34:46 +0000 (16:34 -0400)
These new exception types are useful to expess communication errors
and are used in later patches when an application doesn't honor the
liblttng-ust-ctl protocol.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: If5cd92c2f2fc31c6e68d49ade9e66f16e50394a3

src/common/exception.cpp
src/common/exception.hpp

index cc0011405a69f37e17711e485e43a050c537295f..241686eaf6f4687038f522ed04ccc8d6d5707239 100644 (file)
@@ -11,7 +11,7 @@
 
 namespace {
 std::string format_throw_location(
-       const char *file_name, const char *function_name, unsigned int line_number)
+               const char *file_name, const char *function_name, unsigned int line_number)
 {
        std::stringstream location;
 
@@ -20,32 +20,55 @@ std::string format_throw_location(
 
        return location.str();
 }
-} // namespace
+} /* namespace */
 
 lttng::ctl::error::error(lttng_error_code error_code,
-       const char *file_name,
-       const char *function_name,
-       unsigned int line_number) :
-       std::runtime_error(std::string(error_get_str(error_code)) + " " +
-               format_throw_location(file_name, function_name, line_number))
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number) :
+       runtime_error(std::string(error_get_str(error_code)), file_name, function_name, line_number)
 {
 }
 
-lttng::posix_error::posix_error(const std::string &msg,
-       int errno_code,
-       const char *file_name,
-       const char *function_name,
-       unsigned int line_number) :
+lttng::posix_error::posix_error(const std::stringmsg,
+               int errno_code,
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number) :
        std::system_error(errno_code,
-               std::generic_category(),
-               msg + " " + format_throw_location(file_name, function_name, line_number))
+                       std::generic_category(),
+                       msg + " " + format_throw_location(file_name, function_name, line_number))
 {
 }
 
-lttng::runtime_error::runtime_error(const std::string &msg,
-       const char *file_name,
-       const char *function_name,
-       unsigned int line_number) :
+lttng::runtime_error::runtime_error(const std::stringmsg,
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number) :
        std::runtime_error(msg + " " + format_throw_location(file_name, function_name, line_number))
 {
 }
+
+lttng::communication_error::communication_error(const std::string& msg,
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number) :
+       runtime_error(msg, file_name, function_name, line_number)
+{
+}
+
+lttng::protocol_error::protocol_error(const std::string& msg,
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number) :
+       communication_error(msg, file_name, function_name, line_number)
+{
+}
+
+lttng::invalid_argument_error::invalid_argument_error(const std::string& msg,
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number) :
+       runtime_error(msg, file_name, function_name, line_number)
+{
+}
index b223799151508c5a5a51d37628db093aca9bc6d6..b7e8261ed65217f5652fd88192c37e78c178dd76 100644 (file)
        throw lttng::posix_error(msg, errno_code, __FILE__, __func__, __LINE__)
 #define LTTNG_THROW_ERROR(msg) \
        throw lttng::runtime_error(msg, __FILE__, __func__, __LINE__)
+#define LTTNG_THROW_COMMUNICATION_ERROR(msg) \
+       throw lttng::communication_error(msg, __FILE__, __func__, __LINE__)
+#define LTTNG_THROW_PROTOCOL_ERROR(msg) \
+       throw lttng::protocol_error(msg, __FILE__, __func__, __LINE__)
+#define LTTNG_THROW_INVALID_ARGUMENT_ERROR(msg) \
+       throw lttng::invalid_argument_error(msg, __FILE__, __func__, __LINE__)
 
 namespace lttng {
+class runtime_error : public std::runtime_error {
+public:
+       explicit runtime_error(const std::string& msg,
+                       const char *file_name,
+                       const char *function_name,
+                       unsigned int line_number);
+};
 
 namespace ctl {
 /* Wrap lttng_error_code errors which may be reported through liblttng-ctl's interface. */
-class error : public std::runtime_error {
+class error : public runtime_error {
 public:
        explicit error(lttng_error_code error_code,
-               const char *file_name,
-               const char *function_name,
-               unsigned int line_number);
+                       const char *file_name,
+                       const char *function_name,
+                       unsigned int line_number);
        lttng_error_code get_code() const;
 
 private:
@@ -40,19 +53,35 @@ private:
 
 class posix_error : public std::system_error {
 public:
-       explicit posix_error(const std::string &msg,
-               int errno_code,
-               const char *file_name,
-               const char *function_name,
-               unsigned int line_number);
+       explicit posix_error(const std::stringmsg,
+                       int errno_code,
+                       const char *file_name,
+                       const char *function_name,
+                       unsigned int line_number);
 };
 
-class runtime_error : public std::runtime_error {
+class communication_error : public runtime_error {
+public:
+       explicit communication_error(const std::string& msg,
+                       const char *file_name,
+                       const char *function_name,
+                       unsigned int line_number);
+};
+
+class protocol_error : public communication_error {
+public:
+       explicit protocol_error(const std::string& msg,
+                       const char *file_name,
+                       const char *function_name,
+                       unsigned int line_number);
+};
+
+class invalid_argument_error : public runtime_error {
 public:
-       explicit runtime_error(const std::string &msg,
-               const char *file_name,
-               const char *function_name,
-               unsigned int line_number);
+       explicit invalid_argument_error(const std::string& msg,
+                       const char *file_name,
+                       const char *function_name,
+                       unsigned int line_number);
 };
 
 }; /* namespace lttng */
This page took 0.028762 seconds and 4 git commands to generate.