diff --git a/docs/api/reference.rst b/docs/api/reference.rst index 77f7bd91..532e6d57 100644 --- a/docs/api/reference.rst +++ b/docs/api/reference.rst @@ -56,3 +56,5 @@ LOOT uses error category objects to identify errors with codes that originate in lower-level libraries. .. doxygenfunction:: loot::libloadorder_category + +.. doxygenfunction:: loot::libgit2_category diff --git a/include/loot/error_categories.h b/include/loot/error_categories.h index 5e4f0bba..5518dc34 100644 --- a/include/loot/error_categories.h +++ b/include/loot/error_categories.h @@ -36,6 +36,13 @@ namespace loot { derived from std::error_category. */ LOOT_API const std::error_category& libloadorder_category(); + +/** @brief Get the error category that can be used to identify system_error + * exceptions that are due to libgit2 errors. + * @returns A reference to the static object of unspecified runtime type, + derived from std::error_category. + */ +LOOT_API const std::error_category& libgit2_category(); } #endif diff --git a/src/backend/error_categories.cpp b/src/backend/error_categories.cpp index a49a5dcc..965207c8 100644 --- a/src/backend/error_categories.cpp +++ b/src/backend/error_categories.cpp @@ -39,10 +39,29 @@ class libloadorder_category : public std::error_category { return code.category().name() == name(); } }; + +class libgit2_category : public std::error_category { + virtual const char* name() const noexcept { + return "libgit2"; + } + + virtual std::string message(int ev) const { + return "libgit2 error"; + } + + virtual bool equivalent(const std::error_code& code, int condition) const noexcept { + return code.category().name() == name(); + } +}; } LOOT_API const std::error_category& libloadorder_category() { static detail::libloadorder_category instance; return instance; } + +LOOT_API const std::error_category& libgit2_category() { + static detail::libgit2_category instance; + return instance; +} } diff --git a/src/backend/helpers/git_helper.cpp b/src/backend/helpers/git_helper.cpp index d72a89cc..71f55bc5 100644 --- a/src/backend/helpers/git_helper.cpp +++ b/src/backend/helpers/git_helper.cpp @@ -29,6 +29,7 @@ #include #include "loot/error.h" +#include "loot/error_categories.h" using boost::locale::translate; using std::string; @@ -104,7 +105,7 @@ void GitHelper::Call(int error_code) { errorMessage_ = (boost::format(translate("Git operation failed. Error: %1%")) % gitError).str(); BOOST_LOG_TRIVIAL(error) << "Git operation failed. Error: " << gitError; - throw Error(Error::Code::git_error, errorMessage_); + throw std::system_error(error_code, libgit2_category(), errorMessage_); } void GitHelper::SetErrorMessage(const std::string& message) { diff --git a/src/tests/backend/helpers/git_helper_test.h b/src/tests/backend/helpers/git_helper_test.h index 8954f6e3..e175aeca 100644 --- a/src/tests/backend/helpers/git_helper_test.h +++ b/src/tests/backend/helpers/git_helper_test.h @@ -94,8 +94,8 @@ TEST_F(GitHelperTest, callShouldNotThrowIfPassedAZeroValue) { } TEST_F(GitHelperTest, callShouldThrowIfPassedANonZeroValue) { - EXPECT_THROW(git_.Call(1), Error); - EXPECT_THROW(git_.Call(-1), Error); + EXPECT_THROW(git_.Call(1), std::system_error); + EXPECT_THROW(git_.Call(-1), std::system_error); } TEST_F(GitHelperTest, setErrorMessageShouldSetTheMessageForThrownExceptions) { @@ -105,7 +105,7 @@ TEST_F(GitHelperTest, setErrorMessageShouldSetTheMessageForThrownExceptions) { try { git_.Call(1); ADD_FAILURE() << "An exception should have been thrown."; - } catch (Error& e) { + } catch (std::system_error& e) { EXPECT_NE(nullptr, strstr(e.what(), errorMessage)); } }