Add basic exception types and throwing facilities
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 29 Apr 2022 19:43:14 +0000 (15:43 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 13 Jun 2022 20:34:46 +0000 (16:34 -0400)
Add two LTTng-specific exception types:
  - lttng::ctl::error
  - lttng::posix_error

These types are meant to help transition from error code-based
error handling in RAII-safe functions.

lttng::ctl::error wraps `enum lttng_error_code`. It is meant to be
thrown using the `LTTNG_THROW_CTL` macro which samples the throw-site
(file name, function name, line number). This should be used only
in code paths dealing providing the liblttng-ctl interface.

It should, ultimately, be thrown in code that is specific to the
implementation of the various liblttng-ctl commands and not all over the
place since it contains very little information beyond the error code.

lttng::posix_error wraps `errno` values that are used in various places
to report errors involving (mostly) syscalls.

Over time, more specific exception types will be added.

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

src/common/Makefile.am
src/common/exception.cpp [new file with mode: 0644]
src/common/exception.hpp [new file with mode: 0644]

index 928f8d30b0b97fe535a81950ad68b7216fa640f9..1a42c7c73f7bfffaa28d06b3a2e91591ae894f71 100644 (file)
@@ -83,6 +83,7 @@ libcommon_lgpl_la_SOURCES = \
        event-rule/log4j-logging.cpp \
        event-rule/jul-logging.cpp \
        event-rule/python-logging.cpp \
+       exception.cpp exception.hpp \
        fd-handle.cpp fd-handle.hpp\
        kernel-probe.cpp \
        location.cpp \
diff --git a/src/common/exception.cpp b/src/common/exception.cpp
new file mode 100644 (file)
index 0000000..16e27e5
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include "exception.hpp"
+#include <sstream>
+#include <common/error.hpp>
+
+namespace {
+std::string format_throw_location(
+       const char *file_name, const char *function_name, unsigned int line_number)
+{
+       std::stringstream location;
+
+       location << "[" << function_name << "()"
+                << " " << file_name << ":" << line_number << "]";
+
+       return location.str();
+}
+} // 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))
+{
+}
+
+lttng::posix_error::posix_error(const std::string &msg,
+       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))
+{
+}
diff --git a/src/common/exception.hpp b/src/common/exception.hpp
new file mode 100644 (file)
index 0000000..20731c0
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_EXCEPTION_H_
+#define LTTNG_EXCEPTION_H_
+
+#include <string>
+#include <stdexcept>
+#include <system_error>
+
+#include <lttng/lttng-error.h>
+
+#define LTTNG_THROW_CTL(error_code) \
+       throw lttng::ctl::error(msg, error_code, __FILE__, __func__, __LINE__)
+#define LTTNG_THROW_POSIX(msg, errno_code) \
+       throw lttng::posix_error(msg, errno_code, __FILE__, __func__, __LINE__)
+
+namespace lttng {
+
+namespace ctl {
+/* Wrap lttng_error_code errors which may be reported through liblttng-ctl's interface. */
+class error : public std::runtime_error {
+public:
+       explicit error(lttng_error_code error_code,
+               const char *file_name,
+               const char *function_name,
+               unsigned int line_number);
+       lttng_error_code get_code() const;
+
+private:
+       lttng_error_code _error_code;
+};
+} /* namespace ctl */
+
+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);
+};
+
+}; /* namespace lttng */
+
+#endif /* LTTNG_EXCEPTION_H_ */
This page took 0.027294 seconds and 4 git commands to generate.