Replace Error::Code::ok

Throw more suitable exceptions instead when appropriate or handle the
state without throwing.
This commit is contained in:
Oliver Hamlet
2016-10-08 12:21:07 +01:00
parent a5e3905505
commit 2ec9ab29ac
7 changed files with 28 additions and 40 deletions
+9 -14
View File
@@ -117,21 +117,16 @@ MasterlistInfo ApiDatabase::GetMasterlistRevision(const std::string& masterlistP
const bool getShortID) {
MasterlistInfo apiMasterlistInfo;
apiMasterlistInfo.is_modified = false;
try {
Masterlist::Info info = Masterlist::GetInfo(masterlistPath, getShortID);
Masterlist::Info info = Masterlist::GetInfo(masterlistPath, getShortID);
if (boost::ends_with(info.revision, " (edited)")) {
apiMasterlistInfo.revision_id = info.revision.substr(0, info.revision.length() - 9);
apiMasterlistInfo.revision_date = info.date.substr(0, info.date.length() - 9);
apiMasterlistInfo.is_modified = true;
} else {
apiMasterlistInfo.revision_id = info.revision;
apiMasterlistInfo.revision_date = info.date;
apiMasterlistInfo.is_modified = false;
}
} catch (Error &e) {
if (e.code() != Error::Code::ok)
throw;
if (boost::ends_with(info.revision, " (edited)")) {
apiMasterlistInfo.revision_id = info.revision.substr(0, info.revision.length() - 9);
apiMasterlistInfo.revision_date = info.date.substr(0, info.date.length() - 9);
apiMasterlistInfo.is_modified = true;
} else {
apiMasterlistInfo.revision_id = info.revision;
apiMasterlistInfo.revision_date = info.date;
apiMasterlistInfo.is_modified = false;
}
return apiMasterlistInfo;
+1 -1
View File
@@ -307,7 +307,7 @@ GitHelper::GitData& GitHelper::GetData() {
bool GitHelper::IsFileDifferent(const boost::filesystem::path& repoRoot, const std::string& filename) {
if (!IsRepository(repoRoot)) {
BOOST_LOG_TRIVIAL(info) << "Unknown masterlist revision: Git repository missing.";
throw Error(Error::Code::ok, translate("Unknown: Git repository missing"));
throw GitStateError("Cannot check if the \"" + filename + "\" working copy is edited, Git repository missing.");
}
BOOST_LOG_TRIVIAL(debug) << "Existing repository found, attempting to open it.";
+5 -4
View File
@@ -26,7 +26,8 @@
#include <boost/log/trivial.hpp>
#include "loot/error.h"
#include "loot/exception/file_access_error.h"
#include "loot/exception/git_state_error.h"
#include "backend/game/game.h"
#include "backend/helpers/git_helper.h"
@@ -44,10 +45,10 @@ Masterlist::Info Masterlist::GetInfo(const boost::filesystem::path& path, bool s
if (!fs::exists(path)) {
BOOST_LOG_TRIVIAL(info) << "Unknown masterlist revision: No masterlist present.";
throw Error(Error::Code::ok, translate("N/A: No masterlist present"));
throw FileAccessError(translate("N/A: No masterlist present"));
} else if (!git.IsRepository(path.parent_path())) {
BOOST_LOG_TRIVIAL(info) << "Unknown masterlist revision: Git repository missing.";
throw Error(Error::Code::ok, translate("Unknown: Git repository missing"));
throw GitStateError(translate("Unknown: Git repository missing"));
}
BOOST_LOG_TRIVIAL(debug) << "Existing repository found, attempting to open it.";
@@ -263,7 +264,7 @@ bool Masterlist::Update(const boost::filesystem::path& path, const std::string&
} while (parsingFailed);
if (!parsingError.empty())
throw Error(Error::Code::ok, parsingError); //Throw an OK because the process still completed in a successful state.
AppendMessage(Message(MessageType::error, parsingError));
return true;
}
+4 -3
View File
@@ -48,6 +48,8 @@ typedef boost::graph_traits<PluginGraph>::vertex_iterator vertex_it;
typedef boost::graph_traits<PluginGraph>::edge_descriptor edge_t;
typedef boost::graph_traits<PluginGraph>::edge_iterator edge_it;
class PathFoundException : public std::exception {};
class CycleDetector : public boost::dfs_visitor<> {
public:
void tree_edge(edge_t edge, const PluginGraph& graph) {
@@ -93,7 +95,7 @@ public:
inline void discover_vertex(vertex_t vertex, const PluginGraph& graph) {
if (vertex == target)
throw Error(Error::Code::ok, "Found a path.");
throw PathFoundException();
}
private:
@@ -248,8 +250,7 @@ void PluginSorter::CheckForCycles() const {
bool PluginSorter::EdgeCreatesCycle(const vertex_t& fromVertex, const vertex_t& toVertex) const {
try {
boost::breadth_first_search(graph_, toVertex, visitor(PathDetector(fromVertex)).vertex_index_map(vertexIndexMap_));
} catch (Error& e) {
if (e.code() == Error::Code::ok)
} catch (PathFoundException& e) {
return true;
}
return false;
+3 -12
View File
@@ -51,18 +51,9 @@ private:
try {
return game_.GetMasterlist().Update(game_);
} catch (Error &e) {
if (e.code() == Error::Code::ok) {
// There was a parsing error, but roll-back was successful, so the
// process should still complete.
game_.GetMasterlist().AppendMessage(Message(MessageType::error, e.what()));
return true;
} else {
// Error wasn't a parsing error. Need to try parsing masterlist if it
// exists.
try {
game_.GetMasterlist().Load(game_.MasterlistPath());
} catch (...) {}
}
try {
game_.GetMasterlist().Load(game_.MasterlistPath());
} catch (...) {}
throw;
}
}
+4 -4
View File
@@ -209,19 +209,19 @@ TEST_P(DatabaseInterfaceTest, updateMasterlistShouldSucceedIfCalledRepeatedlyBut
EXPECT_TRUE(boost::filesystem::exists(masterlistPath));
}
TEST_P(DatabaseInterfaceTest, getMasterlistRevisionShouldSucceedIfNoMasterlistIsPresent) {
TEST_P(DatabaseInterfaceTest, getMasterlistRevisionShouldThrowIfNoMasterlistIsPresent) {
MasterlistInfo info;
EXPECT_NO_THROW(info = db_->GetMasterlistRevision(masterlistPath.string(), false));
EXPECT_THROW(info = db_->GetMasterlistRevision(masterlistPath.string(), false), FileAccessError);
EXPECT_TRUE(info.revision_id.empty());
EXPECT_TRUE(info.revision_date.empty());
EXPECT_FALSE(info.is_modified);
}
TEST_P(DatabaseInterfaceTest, getMasterlistRevisionShouldSucceedIfANonVersionControlledMasterlistIsPresent) {
TEST_P(DatabaseInterfaceTest, getMasterlistRevisionShouldThrowIfANonVersionControlledMasterlistIsPresent) {
ASSERT_NO_THROW(GenerateMasterlist());
MasterlistInfo info;
EXPECT_NO_THROW(info = db_->GetMasterlistRevision(masterlistPath.string(), false));
EXPECT_THROW(info = db_->GetMasterlistRevision(masterlistPath.string(), false), GitStateError);
EXPECT_TRUE(info.revision_id.empty());
EXPECT_TRUE(info.revision_date.empty());
EXPECT_FALSE(info.is_modified);
+2 -2
View File
@@ -29,7 +29,7 @@ along with LOOT. If not, see
#include <gtest/gtest.h>
#include "loot/error.h"
#include "loot/exception/git_state_error.h"
namespace loot {
namespace test {
@@ -119,7 +119,7 @@ TEST_F(GitHelperTest, isRepositoryShouldReturnFalseForRepositorySubdirectory) {
}
TEST_F(GitHelperTest, isFileDifferentShouldThrowIfGivenANonRepositoryPath) {
EXPECT_THROW(GitHelper::IsFileDifferent(boost::filesystem::current_path(), "README.md"), Error);
EXPECT_THROW(GitHelper::IsFileDifferent(boost::filesystem::current_path(), "README.md"), GitStateError);
}
TEST_F(GitHelperTest, isFileDifferentShouldReturnFalseForAnUntrackedFile) {