Preserve libgit2 error codes

Throw a system_error with a libgit2_category error category for errors
that propagate from libgit2.
This commit is contained in:
Oliver Hamlet
2016-10-08 12:10:21 +01:00
parent 5cf7ae14e4
commit 711ba9b59d
5 changed files with 33 additions and 4 deletions
+2
View File
@@ -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
+7
View File
@@ -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
+19
View File
@@ -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;
}
}
+2 -1
View File
@@ -29,6 +29,7 @@
#include <boost/log/trivial.hpp>
#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) {
+3 -3
View File
@@ -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));
}
}