mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user