-
Notifications
You must be signed in to change notification settings - Fork 7
feat(error_handling): Migrate TraceableException from CLP with changes:
#43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidlion
merged 12 commits into
y-scope:main
from
Bill-hbrhbr:add-traceable-exception-lib
Apr 8, 2025
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a249239
Add traceable exception
Bill-hbrhbr 1193425
Update
Bill-hbrhbr 74e4b45
Fix clang tidy issues
Bill-hbrhbr 424b310
lint fix
Bill-hbrhbr 369da3f
lint fix
Bill-hbrhbr e390fcf
Remove line and column number tests
Bill-hbrhbr 6e4e637
Merge branch 'main' into add-traceable-exception-lib
Bill-hbrhbr 9b24006
Use full path and remove all features for specifying relative paths. …
Bill-hbrhbr 51ef54b
Remove cmake definitions
Bill-hbrhbr 193fe11
Remove template-based type of TraceableException
Bill-hbrhbr 57b0937
Update src/ystdlib/error_handling/TraceableException.hpp
Bill-hbrhbr a30d207
Change constructor param to be more descriptive
Bill-hbrhbr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #ifndef YSTDLIB_ERROR_HANDLING_TRACEABLEEXCEPTION_HPP | ||
| #define YSTDLIB_ERROR_HANDLING_TRACEABLEEXCEPTION_HPP | ||
|
|
||
| #include <exception> | ||
| #include <source_location> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <system_error> | ||
| #include <utility> | ||
|
|
||
| #include "types.hpp" | ||
| #include "utils.hpp" | ||
|
|
||
| namespace ystdlib::error_handling { | ||
| /** | ||
| * An exception class that is thrown with an `std::error_code`. | ||
| * | ||
| * This class extends `std::exception` and can be thrown with an `std::error_code` argument. It also | ||
| * provides additional information to aid in debugging by storing details in `std::source_location`, | ||
| * including the function name, file name, and line number of the throwing location. | ||
| * | ||
| * @see std::source_location::file_name() | ||
| * @see std::source_location::function_name() | ||
| * @see std::source_location::line() | ||
| */ | ||
| template <ErrorCodeType ErrorType = std::error_code> | ||
| class TraceableException : public std::exception { | ||
| public: | ||
| // Constructors | ||
| explicit TraceableException( | ||
| ErrorType error_code, | ||
| std::source_location const& where = std::source_location::current() | ||
| ) | ||
| : m_error_code{std::move(error_code)}, | ||
| m_where{where} { | ||
| std::ostringstream oss; | ||
| oss << where; | ||
| m_what = oss.str(); | ||
| } | ||
|
|
||
| explicit TraceableException( | ||
| ErrorType error_code, | ||
| std::string what, | ||
davidlion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::source_location const& where = std::source_location::current() | ||
| ) | ||
| : m_error_code{std::move(error_code)}, | ||
| m_what{std::move(what)}, | ||
| m_where{where} {} | ||
|
|
||
| // Methods implementing std::exception | ||
| [[nodiscard]] auto what() const noexcept -> char const* override { return m_what.c_str(); } | ||
|
|
||
| // Methods | ||
| [[nodiscard]] auto error_code() const -> ErrorType { return m_error_code; } | ||
|
|
||
| [[nodiscard]] auto what() -> std::string& { return m_what; } | ||
|
|
||
| [[nodiscard]] auto where() const noexcept -> std::source_location const& { return m_where; } | ||
|
|
||
| private: | ||
| // Variables | ||
| ErrorType m_error_code; | ||
| std::string m_what; | ||
| std::source_location m_where; | ||
| }; | ||
| } // namespace ystdlib::error_handling | ||
|
|
||
| /** | ||
| * The macro to define a `TraceableException` class with the given class name T. | ||
| */ | ||
| // NOLINTBEGIN(bugprone-macro-parentheses, cppcoreguidelines-macro-usage) | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(T) \ | ||
| class T : public ystdlib::error_handling::TraceableException<> { \ | ||
| using ystdlib::error_handling::TraceableException<>::TraceableException; \ | ||
| } | ||
|
|
||
| #define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION_WITH_ERROR_TYPE(T, E) \ | ||
| class T : public ystdlib::error_handling::TraceableException<E> { \ | ||
| using ystdlib::error_handling::TraceableException<E>::TraceableException; \ | ||
| } | ||
| // NOLINTEND(bugprone-macro-parentheses, cppcoreguidelines-macro-usage) | ||
|
|
||
| #endif // YSTDLIB_ERROR_HANDLING_TRACEABLEEXCEPTION_HPP | ||
69 changes: 69 additions & 0 deletions
69
src/ystdlib/error_handling/test/test_TraceableException.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #include <cassert> | ||
| #include <cstring> | ||
|
|
||
| #include <ystdlib/error_handling/TraceableException.hpp> | ||
| #include <ystdlib/error_handling/types.hpp> | ||
|
|
||
| #include <catch2/catch_test_macros.hpp> | ||
|
|
||
| #include "types.hpp" | ||
|
|
||
| using ystdlib::error_handling::ErrorCodeType; | ||
| using ystdlib::error_handling::TraceableException; | ||
|
|
||
| namespace { | ||
| constexpr auto cCustomFailureDescription{"This operation failed due to invalid args."}; | ||
| constexpr auto cCurrentFileName{"src/ystdlib/error_handling/test/test_TraceableException.cpp"}; | ||
| constexpr auto cSuccessFuncName{ | ||
| "static void ystdlib::error_handling::test::Worker::execute_with_success()" | ||
| }; | ||
| constexpr auto cFailureFuncName{ | ||
| "static void ystdlib::error_handling::test::Worker::execute_with_failure()" | ||
| }; | ||
| } // namespace | ||
|
|
||
| namespace ystdlib::error_handling::test { | ||
| class Worker { | ||
| public: | ||
| YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(OperationFailed); | ||
|
|
||
| static auto execute_with_success() -> void { | ||
| throw OperationFailed(BinaryErrorCode{BinaryErrorCodeEnum::Success}); | ||
| } | ||
|
|
||
| static auto execute_with_failure() -> void { | ||
| throw OperationFailed( | ||
| BinaryErrorCode{BinaryErrorCodeEnum::Failure}, | ||
| cCustomFailureDescription | ||
| ); | ||
| } | ||
| }; | ||
| } // namespace ystdlib::error_handling::test | ||
|
|
||
| namespace { | ||
| template <ErrorCodeType ErrorType = std::error_code, typename Callable> | ||
| [[nodiscard]] auto capture_exception(Callable&& f) -> TraceableException<ErrorType>; | ||
|
|
||
| template <ErrorCodeType ErrorType, typename Callable> | ||
| auto capture_exception(Callable&& f) -> TraceableException<ErrorType> { | ||
| try { | ||
| std::forward<Callable>(f)(); | ||
| } catch (TraceableException<ErrorType>& e) { | ||
| return e; | ||
| } | ||
| assert(false && "The function is expected to throw."); | ||
| } | ||
| } // namespace | ||
|
|
||
| namespace ystdlib::error_handling::test { | ||
| TEST_CASE("test_traceable_exception", "[error_handling][TraceableException]") { | ||
| auto const ex_success{capture_exception<>(Worker::execute_with_success)}; | ||
| REQUIRE(std::string{ex_success.where().file_name()}.ends_with(cCurrentFileName)); | ||
| REQUIRE((0 == std::strcmp(ex_success.where().function_name(), cSuccessFuncName))); | ||
|
|
||
| auto const ex_failure{capture_exception<>(Worker::execute_with_failure)}; | ||
| REQUIRE((0 == std::strcmp(ex_failure.what(), cCustomFailureDescription))); | ||
| REQUIRE(std::string{ex_failure.where().file_name()}.ends_with(cCurrentFileName)); | ||
| REQUIRE((0 == std::strcmp(ex_failure.where().function_name(), cFailureFuncName))); | ||
| } | ||
| } // namespace ystdlib::error_handling::test |
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #ifndef YSTDLIB_ERROR_HANDLING_TYPES_HPP | ||
| #define YSTDLIB_ERROR_HANDLING_TYPES_HPP | ||
|
|
||
| #include <concepts> | ||
| #include <system_error> | ||
|
|
||
| namespace ystdlib::error_handling { | ||
| /** | ||
| * Concept that defines a template parameter of an integer-based error code enumeration. | ||
| * @tparam Type | ||
| */ | ||
| template <typename Type> | ||
| concept ErrorCodeType | ||
| = std::same_as<Type, std::error_code> || std::convertible_to<Type, std::error_code>; | ||
| } // namespace ystdlib::error_handling | ||
|
|
||
| #endif // YSTDLIB_ERROR_HANDLING_TYPES_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #ifndef YSTDLIB_ERROR_HANDLING_UTILS_HPP | ||
| #define YSTDLIB_ERROR_HANDLING_UTILS_HPP | ||
|
|
||
| #include <ostream> | ||
| #include <source_location> | ||
|
|
||
| namespace ystdlib::error_handling { | ||
| inline auto operator<<(std::ostream& os, std::source_location const& where) -> std::ostream& { | ||
| return os << where.file_name() << "(" << where.line() << ":" << where.column() | ||
| << "), function `" << where.function_name() << "`"; | ||
| } | ||
| } // namespace ystdlib::error_handling | ||
|
|
||
| #endif // YSTDLIB_ERROR_HANDLING_UTILS_HPP |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.