From 39278ebc70d57de534466c533d6c543a318dfdac Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 30 May 2022 07:46:26 -0400 Subject: [PATCH] cpp-common/bt2: use classes from `cpp-common/exc.hpp` This patch: * Renames `lib-error.hpp` to `exc.hpp` as this could contain non-error exceptions in the future. * Changes `bt2::Error`, `bt2::MemoryError`, and `bt2::OverflowError` to be aliases of `bt2_common::Error`, `bt2_common::MemoryError`, and `bt2_common::OverflowError`. This makes it possible for some code to catch only the common `bt2_common` errors to handle both library and user/internal exceptions. Note that those exceptions don't carry much information themselves; they're usually just a signal that an exception occurred, while the details are part of the libbabeltrace2 error of the current thread. Signed-off-by: Philippe Proulx Change-Id: I5bc1d39c8d978c21604614c68012509edc1776d5 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8176 Reviewed-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/10824 Tested-by: jenkins --- src/cpp-common/bt2/clock-class.hpp | 8 ++-- src/cpp-common/bt2/clock-snapshot.hpp | 4 +- src/cpp-common/bt2/exc.hpp | 23 +++++++++++ src/cpp-common/bt2/field-class.hpp | 10 ++--- src/cpp-common/bt2/field.hpp | 8 ++-- src/cpp-common/bt2/integer-range-set.hpp | 4 +- src/cpp-common/bt2/internal/utils.hpp | 4 +- src/cpp-common/bt2/lib-error.hpp | 50 ------------------------ src/cpp-common/bt2/message.hpp | 2 +- src/cpp-common/bt2/trace-ir.hpp | 22 +++++------ src/cpp-common/bt2/value.hpp | 12 +++--- 11 files changed, 60 insertions(+), 87 deletions(-) create mode 100644 src/cpp-common/bt2/exc.hpp delete mode 100644 src/cpp-common/bt2/lib-error.hpp diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index 8e45e2f2..e80c8f9d 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -18,7 +18,7 @@ #include "cpp-common/optional.hpp" #include "cpp-common/string_view.hpp" #include "cpp-common/uuid-view.hpp" -#include "lib-error.hpp" +#include "exc.hpp" #include "value.hpp" namespace bt2 { @@ -181,7 +181,7 @@ public: const auto status = bt_clock_class_set_name(this->libObjPtr(), name); if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -208,7 +208,7 @@ public: const auto status = bt_clock_class_set_description(this->libObjPtr(), description); if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -271,7 +271,7 @@ public: bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin); if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) { - throw LibOverflowError {}; + throw OverflowError {}; } return nsFromOrigin; diff --git a/src/cpp-common/bt2/clock-snapshot.hpp b/src/cpp-common/bt2/clock-snapshot.hpp index 9bd7a6d6..643a229e 100644 --- a/src/cpp-common/bt2/clock-snapshot.hpp +++ b/src/cpp-common/bt2/clock-snapshot.hpp @@ -11,7 +11,7 @@ #include #include "internal/borrowed-obj.hpp" -#include "lib-error.hpp" +#include "exc.hpp" namespace bt2 { @@ -49,7 +49,7 @@ public: const auto status = bt_clock_snapshot_get_ns_from_origin(this->libObjPtr(), &nsFromOrigin); if (status == BT_CLOCK_SNAPSHOT_GET_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) { - throw LibOverflowError {}; + throw OverflowError {}; } return nsFromOrigin; diff --git a/src/cpp-common/bt2/exc.hpp b/src/cpp-common/bt2/exc.hpp new file mode 100644 index 00000000..4b57e43b --- /dev/null +++ b/src/cpp-common/bt2/exc.hpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2020-2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_EXC_HPP +#define BABELTRACE_CPP_COMMON_BT2_EXC_HPP + +#include +#include + +#include "cpp-common/exc.hpp" + +namespace bt2 { + +using Error = bt2_common::Error; +using OverflowError = bt2_common::OverflowError; +using MemoryError = bt2_common::MemoryError; + +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_EXC_HPP */ diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index 6460000f..536401c4 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -18,7 +18,7 @@ #include "cpp-common/optional.hpp" #include "cpp-common/string_view.hpp" #include "common-iter.hpp" -#include "lib-error.hpp" +#include "exc.hpp" #include "integer-range-set.hpp" #include "field-path.hpp" #include "value.hpp" @@ -886,7 +886,7 @@ public: this->libObjPtr(), label, ranges.libObjPtr()); if (status == BT_FIELD_CLASS_ENUMERATION_ADD_MAPPING_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1175,7 +1175,7 @@ public: bt_field_class_structure_append_member(this->libObjPtr(), name, fc.libObjPtr()); if (status == BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -2351,7 +2351,7 @@ public: if (status == BT_FIELD_CLASS_VARIANT_WITHOUT_SELECTOR_FIELD_APPEND_OPTION_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -2579,7 +2579,7 @@ public: if (status == BT_FIELD_CLASS_VARIANT_WITH_SELECTOR_FIELD_APPEND_OPTION_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index f1908b99..126f8aa8 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -662,7 +662,7 @@ public: &labelArray, &count); if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } return EnumerationFieldClassMappingLabels {labelArray, count}; @@ -743,7 +743,7 @@ public: bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count); if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } return EnumerationFieldClassMappingLabels {labelArray, count}; @@ -951,7 +951,7 @@ public: const auto status = bt_field_string_set_value(this->libObjPtr(), val); if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } return *this; @@ -1298,7 +1298,7 @@ public: const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length); if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } }; diff --git a/src/cpp-common/bt2/integer-range-set.hpp b/src/cpp-common/bt2/integer-range-set.hpp index 5889972d..a2cf1192 100644 --- a/src/cpp-common/bt2/integer-range-set.hpp +++ b/src/cpp-common/bt2/integer-range-set.hpp @@ -15,7 +15,7 @@ #include "internal/borrowed-obj.hpp" #include "internal/utils.hpp" #include "integer-range.hpp" -#include "lib-error.hpp" +#include "exc.hpp" namespace bt2 { @@ -226,7 +226,7 @@ public: const auto status = _Spec::addRange(this->libObjPtr(), lower, upper); if (status == BT_INTEGER_RANGE_SET_ADD_RANGE_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } diff --git a/src/cpp-common/bt2/internal/utils.hpp b/src/cpp-common/bt2/internal/utils.hpp index 34c52fff..7b162d12 100644 --- a/src/cpp-common/bt2/internal/utils.hpp +++ b/src/cpp-common/bt2/internal/utils.hpp @@ -9,7 +9,7 @@ #include -#include "../lib-error.hpp" +#include "../exc.hpp" namespace bt2 { namespace internal { @@ -18,7 +18,7 @@ template void validateCreatedObjPtr(const LibObjPtrT libOjbPtr) { if (!libOjbPtr) { - throw LibMemoryError {}; + throw MemoryError {}; } } diff --git a/src/cpp-common/bt2/lib-error.hpp b/src/cpp-common/bt2/lib-error.hpp deleted file mode 100644 index be8f8000..00000000 --- a/src/cpp-common/bt2/lib-error.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2020 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_BT2_LIB_ERROR_HPP -#define BABELTRACE_CPP_COMMON_BT2_LIB_ERROR_HPP - -#include -#include - -namespace bt2 { - -/* - * Any library error. - */ -class LibError : public std::runtime_error -{ -public: - explicit LibError(const std::string& msg = "Error") : std::runtime_error {msg} - { - } -}; - -/* - * Memory error. - */ -class LibMemoryError : public LibError -{ -public: - LibMemoryError() : LibError {"Memory error"} - { - } -}; - -/* - * Overflow error. - */ -class LibOverflowError : public LibError -{ -public: - LibOverflowError() : LibError {"Overflow error"} - { - } -}; - -} /* namespace bt2 */ - -#endif /* BABELTRACE_CPP_COMMON_BT2_LIB_ERROR_HPP */ diff --git a/src/cpp-common/bt2/message.hpp b/src/cpp-common/bt2/message.hpp index bc83c4b2..5f6d3760 100644 --- a/src/cpp-common/bt2/message.hpp +++ b/src/cpp-common/bt2/message.hpp @@ -19,7 +19,7 @@ #include "internal/utils.hpp" #include "cpp-common/optional.hpp" #include "cpp-common/string_view.hpp" -#include "lib-error.hpp" +#include "exc.hpp" namespace bt2 { namespace internal { diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 625c3f33..5881092b 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -546,7 +546,7 @@ public: const auto status = bt_stream_set_name(this->libObjPtr(), name); if (status == BT_STREAM_SET_NAME_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -768,7 +768,7 @@ public: const auto status = bt_trace_set_name(this->libObjPtr(), name); if (status == BT_TRACE_SET_NAME_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -848,7 +848,7 @@ public: const auto status = bt_trace_set_environment_entry_integer(this->libObjPtr(), name, val); if (status == BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -864,7 +864,7 @@ public: const auto status = bt_trace_set_environment_entry_string(this->libObjPtr(), name, val); if (status == BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1121,7 +1121,7 @@ public: const auto status = bt_event_class_set_name(this->libObjPtr(), name); if (status == BT_EVENT_CLASS_SET_NAME_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1168,7 +1168,7 @@ public: const auto status = bt_event_class_set_emf_uri(this->libObjPtr(), emfUri); if (status == BT_EVENT_CLASS_SET_EMF_URI_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1196,7 +1196,7 @@ public: bt_event_class_set_payload_field_class(this->libObjPtr(), fc.libObjPtr()); if (status == BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1230,7 +1230,7 @@ public: bt_event_class_set_specific_context_field_class(this->libObjPtr(), fc.libObjPtr()); if (status == BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1525,7 +1525,7 @@ public: const auto status = bt_stream_class_set_name(this->libObjPtr(), name); if (status == BT_STREAM_CLASS_SET_NAME_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1719,7 +1719,7 @@ public: bt_stream_class_set_packet_context_field_class(this->libObjPtr(), fc.libObjPtr()); if (status == BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } @@ -1753,7 +1753,7 @@ public: bt_stream_class_set_event_common_context_field_class(this->libObjPtr(), fc.libObjPtr()); if (status == BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index a065af6c..41bb4db5 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -21,7 +21,7 @@ #include "internal/utils.hpp" #include "cpp-common/optional.hpp" #include "cpp-common/string_view.hpp" -#include "lib-error.hpp" +#include "exc.hpp" namespace bt2 { namespace internal { @@ -696,7 +696,7 @@ public: const auto status = bt_value_string_set(this->libObjPtr(), rawVal); if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } return *this; @@ -947,7 +947,7 @@ private: void _handleAppendLibStatus(const bt_value_array_append_element_status status) const { if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } }; @@ -1034,7 +1034,7 @@ struct CommonMapValueSpec final return; case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR: case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR: - throw LibError {}; + throw Error {}; default: bt_common_abort(); } @@ -1067,7 +1067,7 @@ struct CommonMapValueSpec final return; case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR: case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR: - throw LibError {}; + throw Error {}; default: bt_common_abort(); } @@ -1286,7 +1286,7 @@ private: void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const { if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } }; -- 2.34.1