From e87c8e772cc6edbbabcd55388122191a473ca138 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Mon, 11 Jul 2016 19:54:46 +0100 Subject: [PATCH] Reimplement error codes as strongly-typed enums --- src/api/api.cpp | 52 ++++++++++--------- src/api/loot_db.cpp | 2 +- src/backend/app/loot_paths.cpp | 2 +- src/backend/app/loot_state.cpp | 4 +- src/backend/error.h | 49 ++++++++++------- src/backend/game/game.cpp | 6 +-- src/backend/game/game_cache.cpp | 2 +- src/backend/game/load_order_handler.cpp | 12 ++--- src/backend/helpers/git_helper.cpp | 28 +++++----- src/backend/helpers/helpers.cpp | 8 +-- src/backend/masterlist.cpp | 8 +-- src/backend/metadata/condition_grammar.h | 14 ++--- src/backend/metadata/conditional_metadata.cpp | 8 +-- src/backend/metadata/message.cpp | 2 +- src/backend/metadata/plugin_metadata.cpp | 2 +- src/backend/metadata_list.cpp | 6 +-- src/backend/plugin/plugin_sorter.cpp | 6 +-- src/gui/query_handler.cpp | 36 ++++++------- 18 files changed, 130 insertions(+), 117 deletions(-) diff --git a/src/api/api.cpp b/src/api/api.cpp index 593c26e7..81b6ca6d 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -41,20 +41,22 @@ #include #include -const unsigned int loot_ok = loot::Error::ok; -const unsigned int loot_error_liblo_error = loot::Error::liblo_error; -const unsigned int loot_error_file_write_fail = loot::Error::path_write_fail; -const unsigned int loot_error_parse_fail = loot::Error::path_read_fail; -const unsigned int loot_error_condition_eval_fail = loot::Error::condition_eval_fail; -const unsigned int loot_error_regex_eval_fail = loot::Error::regex_eval_fail; -const unsigned int loot_error_no_mem = loot::Error::no_mem; -const unsigned int loot_error_invalid_args = loot::Error::invalid_args; -const unsigned int loot_error_no_tag_map = loot::Error::no_tag_map; -const unsigned int loot_error_path_not_found = loot::Error::path_not_found; -const unsigned int loot_error_no_game_detected = loot::Error::no_game_detected; -const unsigned int loot_error_git_error = loot::Error::git_error; -const unsigned int loot_error_windows_error = loot::Error::windows_error; -const unsigned int loot_error_sorting_error = loot::Error::sorting_error; +using loot::Error; + +const unsigned int loot_ok = Error::asUnsignedInt(Error::Code::ok); +const unsigned int loot_error_liblo_error = Error::asUnsignedInt(Error::Code::liblo_error); +const unsigned int loot_error_file_write_fail = Error::asUnsignedInt(Error::Code::path_write_fail); +const unsigned int loot_error_parse_fail = Error::asUnsignedInt(Error::Code::path_read_fail); +const unsigned int loot_error_condition_eval_fail = Error::asUnsignedInt(Error::Code::condition_eval_fail); +const unsigned int loot_error_regex_eval_fail = Error::asUnsignedInt(Error::Code::regex_eval_fail); +const unsigned int loot_error_no_mem = Error::asUnsignedInt(Error::Code::no_mem); +const unsigned int loot_error_invalid_args = Error::asUnsignedInt(Error::Code::invalid_args); +const unsigned int loot_error_no_tag_map = Error::asUnsignedInt(Error::Code::no_tag_map); +const unsigned int loot_error_path_not_found = Error::asUnsignedInt(Error::Code::path_not_found); +const unsigned int loot_error_no_game_detected = Error::asUnsignedInt(Error::Code::no_game_detected); +const unsigned int loot_error_git_error = Error::asUnsignedInt(Error::Code::git_error); +const unsigned int loot_error_windows_error = Error::asUnsignedInt(Error::Code::windows_error); +const unsigned int loot_error_sorting_error = Error::asUnsignedInt(Error::Code::sorting_error); const unsigned int loot_return_max = loot_error_sorting_error; // The following are the games identifiers used by the API. @@ -89,13 +91,13 @@ const unsigned int loot_needs_cleaning_unknown = 2; std::string extMessageStr; -unsigned int c_error(const loot::Error& e) { +unsigned int c_error(const Error& e) { extMessageStr = e.what(); - return e.code(); + return e.codeAsUnsignedInt(); } unsigned int c_error(const unsigned int code, const std::string& what) { - return c_error(loot::Error(code, what.c_str())); + return c_error(Error(Error::Code(code), what.c_str())); } ////////////////////////////// @@ -200,7 +202,7 @@ LOOT_API unsigned int loot_create_db(loot_db ** const db, *db = new loot_db(clientGame, game_path, game_local_path); } - catch (loot::Error& e) { + catch (Error& e) { return c_error(e); } catch (std::bad_alloc& e) { @@ -303,7 +305,7 @@ LOOT_API unsigned int loot_eval_lists(loot_db * const db, const unsigned int lan temp.EvalAllConditions(*db, language); userTemp.EvalAllConditions(*db, language); } - catch (loot::Error& e) { + catch (Error& e) { return c_error(e); } db->GetMasterlist() = temp; @@ -335,7 +337,7 @@ LOOT_API unsigned int loot_sort_plugins(loot_db * const db, db->setPluginNames(sorter.Sort(*db, loot::Language::english)); } - catch (loot::Error &e) { + catch (Error &e) { return c_error(e); } catch (std::bad_alloc& e) { @@ -360,7 +362,7 @@ LOOT_API unsigned int loot_apply_load_order(loot_db * const db, try { db->SetLoadOrder(loadOrder, numPlugins); } - catch (loot::Error &e) { + catch (Error &e) { return c_error(e); } @@ -383,7 +385,7 @@ LOOT_API unsigned int loot_update_masterlist(loot_db * const db, loot::Masterlist masterlist; *updated = masterlist.Update(masterlistPath, remoteURL, remoteBranch); } - catch (loot::Error &e) { + catch (Error &e) { return c_error(e); } @@ -418,8 +420,8 @@ LOOT_API unsigned int loot_get_masterlist_revision(loot_db * const db, db->setRevisionIdString(id); db->setRevisionDateString(date); } - catch (loot::Error &e) { - if (e.code() == loot_ok) + catch (Error &e) { + if (e.code() == Error::Code::ok) return loot_ok; else return c_error(e); @@ -532,7 +534,7 @@ LOOT_API unsigned int loot_get_plugin_tags(loot_db * const db, const char * cons db->setAddedTags(tagsAdded); db->setRemovedTags(tagsRemoved); } - catch (loot::Error& e) { + catch (Error& e) { return c_error(e); } diff --git a/src/api/loot_db.cpp b/src/api/loot_db.cpp index 2edf4d66..25c43fca 100644 --- a/src/api/loot_db.cpp +++ b/src/api/loot_db.cpp @@ -61,7 +61,7 @@ unsigned int loot_db::getBashTagUid(const std::string& name) const { if (it != end(bashTagMap)) return it->second; - throw loot::Error(loot::Error::no_tag_map, "The Bash Tag \"" + name + "\" does not exist in the Bash Tag map."); + throw loot::Error(loot::Error::Code::no_tag_map, "The Bash Tag \"" + name + "\" does not exist in the Bash Tag map."); } const std::vector& loot_db::getAddedTagIds() const { diff --git a/src/backend/app/loot_paths.cpp b/src/backend/app/loot_paths.cpp index 9b0add9d..a1d05a21 100644 --- a/src/backend/app/loot_paths.cpp +++ b/src/backend/app/loot_paths.cpp @@ -81,7 +81,7 @@ namespace loot { PWSTR path; if (SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path) != S_OK) - throw Error(Error::windows_error, boost::locale::translate("Failed to get %LOCALAPPDATA% path.")); + throw Error(Error::Code::windows_error, boost::locale::translate("Failed to get %LOCALAPPDATA% path.")); boost::filesystem::path localAppDataPath(FromWinWide(path)); CoTaskMemFree(path); diff --git a/src/backend/app/loot_state.cpp b/src/backend/app/loot_state.cpp index 1efc9e4c..67c1f478 100644 --- a/src/backend/app/loot_state.cpp +++ b/src/backend/app/loot_state.cpp @@ -187,7 +187,7 @@ namespace loot { storeGameSettings(ToGameSettings(_games)); } catch (loot::Error &e) { - if (e.code() == loot::Error::no_game_detected) { + if (e.code() == loot::Error::Code::no_game_detected) { _initErrors.push_back(e.what()); } else { @@ -270,7 +270,7 @@ namespace loot { // If no game can be selected, throw an exception. if (_currentGame == end(_games)) { BOOST_LOG_TRIVIAL(error) << "None of the supported games were detected."; - throw Error(Error::no_game_detected, translate("None of the supported games were detected.")); + throw Error(Error::Code::no_game_detected, translate("None of the supported games were detected.")); } } diff --git a/src/backend/error.h b/src/backend/error.h index 70919da6..73d14d6d 100644 --- a/src/backend/error.h +++ b/src/backend/error.h @@ -31,30 +31,41 @@ namespace loot { class Error : public std::exception { public: - Error(const unsigned int code_arg, const std::string& what_arg) : _code(code_arg), _what(what_arg) {} + enum struct Code : unsigned int { + // These must not be changed for API stability. + ok = 0, + liblo_error = 1, + path_write_fail = 2, + path_read_fail = 3, + condition_eval_fail = 4, + regex_eval_fail = 5, + no_mem = 6, + invalid_args = 7, + no_tag_map = 8, + path_not_found = 9, + no_game_detected = 10, + //11 was subversion_error, and was removed along with svn support. + git_error = 12, + windows_error = 13, + sorting_error = 14, + }; + + Error(const Code code_arg, const std::string& what_arg) : _code(code_arg), _what(what_arg) {} ~Error() throw() {}; - unsigned int code() const { return _code; } + Code code() const { return _code; } + + unsigned int codeAsUnsignedInt() const { + return asUnsignedInt(_code); + } + const char * what() const throw() { return _what.c_str(); } - /* These must not be changed for API stability. */ - static const unsigned int ok = 0; - static const unsigned int liblo_error = 1; - static const unsigned int path_write_fail = 2; - static const unsigned int path_read_fail = 3; - static const unsigned int condition_eval_fail = 4; - static const unsigned int regex_eval_fail = 5; - static const unsigned int no_mem = 6; - static const unsigned int invalid_args = 7; - static const unsigned int no_tag_map = 8; - static const unsigned int path_not_found = 9; - static const unsigned int no_game_detected = 10; - //11 was subversion_error, and was removed along with svn support. - static const unsigned int git_error = 12; - static const unsigned int windows_error = 13; - static const unsigned int sorting_error = 14; + static unsigned int asUnsignedInt(Code code) { + return static_cast(code); + } private: - unsigned int _code; + Code _code; std::string _what; }; } diff --git a/src/backend/game/game.cpp b/src/backend/game/game.cpp index 1052a3c7..7275f1fe 100644 --- a/src/backend/game/game.cpp +++ b/src/backend/game/game.cpp @@ -55,14 +55,14 @@ namespace loot { void Game::Init(bool createFolder, const boost::filesystem::path& gameLocalAppData) { if (Id() != Game::tes4 && Id() != Game::tes5 && Id() != Game::fo3 && Id() != Game::fonv && Id() != Game::fo4) { - throw Error(Error::invalid_args, lc::translate("Invalid game ID supplied.").str()); + throw Error(Error::Code::invalid_args, lc::translate("Invalid game ID supplied.").str()); } BOOST_LOG_TRIVIAL(info) << "Initialising filesystem-related data for game: " << Name(); if (!this->IsInstalled()) { BOOST_LOG_TRIVIAL(error) << "Game path could not be detected."; - throw Error(Error::path_not_found, lc::translate("Game path could not be detected.").str()); + throw Error(Error::Code::path_not_found, lc::translate("Game path could not be detected.").str()); } if (createFolder) { @@ -73,7 +73,7 @@ namespace loot { } catch (fs::filesystem_error& e) { BOOST_LOG_TRIVIAL(error) << "Could not create LOOT folder for game. Details: " << e.what(); - throw Error(Error::path_write_fail, lc::translate("Could not create LOOT folder for game. Details:").str() + " " + e.what()); + throw Error(Error::Code::path_write_fail, lc::translate("Could not create LOOT folder for game. Details:").str() + " " + e.what()); } } diff --git a/src/backend/game/game_cache.cpp b/src/backend/game/game_cache.cpp index b8da1427..3acaab51 100644 --- a/src/backend/game/game_cache.cpp +++ b/src/backend/game/game_cache.cpp @@ -101,7 +101,7 @@ namespace loot { if (it != end(plugins)) return it->second; - throw Error(Error::invalid_args, "No plugin \"" + pluginName + "\" exists."); + throw Error(Error::Code::invalid_args, "No plugin \"" + pluginName + "\" exists."); } void GameCache::AddPlugin(const Plugin&& plugin) { diff --git a/src/backend/game/load_order_handler.cpp b/src/backend/game/load_order_handler.cpp index 13d73bba..f52f8f6f 100644 --- a/src/backend/game/load_order_handler.cpp +++ b/src/backend/game/load_order_handler.cpp @@ -47,12 +47,12 @@ namespace loot { && game.Id() != GameSettings::fo3 && game.Id() != GameSettings::fonv && game.Id() != GameSettings::fo4) { - throw Error(Error::invalid_args, lc::translate("Unsupported game ID supplied.").str()); + throw Error(Error::Code::invalid_args, lc::translate("Unsupported game ID supplied.").str()); } if (game.GamePath().empty()) { BOOST_LOG_TRIVIAL(error) << "Game path is not initialised."; - throw Error(Error::invalid_args, lc::translate("Game path is not initialised.").str()); + throw Error(Error::Code::invalid_args, lc::translate("Game path is not initialised.").str()); } const char * gameLocalDataPath = nullptr; @@ -93,7 +93,7 @@ namespace loot { err = lc::translate("libloadorder failed to create a game handle. Details:").str() + " " + e; } lo_cleanup(); - throw Error(Error::liblo_error, err); + throw Error(Error::Code::liblo_error, err); } } @@ -115,7 +115,7 @@ namespace loot { err = lc::translate("libloadorder failed to check if a plugin is active. Details:").str() + " " + e; } lo_cleanup(); - throw Error(Error::liblo_error, err); + throw Error(Error::Code::liblo_error, err); } return result; @@ -141,7 +141,7 @@ namespace loot { err = lc::translate("libloadorder failed to get the load order. Details:").str() + " " + e; } lo_cleanup(); - throw Error(Error::liblo_error, err); + throw Error(Error::Code::liblo_error, err); } std::list loadOrder; @@ -168,7 +168,7 @@ namespace loot { err = lc::translate("libloadorder failed to set the load order. Details:").str() + " " + e; } lo_cleanup(); - throw Error(Error::liblo_error, err); + throw Error(Error::Code::liblo_error, err); } } diff --git a/src/backend/helpers/git_helper.cpp b/src/backend/helpers/git_helper.cpp index fe1daeb9..5d972e98 100644 --- a/src/backend/helpers/git_helper.cpp +++ b/src/backend/helpers/git_helper.cpp @@ -104,7 +104,7 @@ namespace loot { errorMessage = (boost::format(lc::translate("Git operation failed. Error: %1%")) % gitError).str(); BOOST_LOG_TRIVIAL(error) << "Git operation failed. Error: " << gitError; - throw loot::Error(loot::Error::git_error, errorMessage); + throw loot::Error(loot::Error::Code::git_error, errorMessage); } void GitHelper::SetErrorMessage(const std::string& message) { @@ -140,7 +140,7 @@ namespace loot { // Clones a repository and opens it. void GitHelper::Clone(const boost::filesystem::path& path, const std::string& url) { if (this->repo != nullptr) - throw Error(Error::git_error, "Cannot clone repository that has already been opened."); + throw Error(Error::Code::git_error, "Cannot clone repository that has already been opened."); this->SetErrorMessage(lc::translate("An error occurred while trying to clone the remote masterlist repository.")); // Clone the remote repository. @@ -191,7 +191,7 @@ namespace loot { void GitHelper::Fetch(const std::string& remote) { if (this->repo == nullptr) - throw Error(Error::git_error, "Cannot fetch updates for repository that has not been opened."); + throw Error(Error::Code::git_error, "Cannot fetch updates for repository that has not been opened."); BOOST_LOG_TRIVIAL(trace) << "Fetching updates from remote."; this->SetErrorMessage(lc::translate("An error occurred while trying to update the masterlist. This could be due to a server-side error. Try again in a few minutes.")); @@ -213,13 +213,13 @@ namespace loot { void GitHelper::CheckoutNewBranch(const std::string& remote, const std::string& branch) { if (this->repo == nullptr) - throw Error(Error::git_error, "Cannot fetch updates for repository that has not been opened."); + throw Error(Error::Code::git_error, "Cannot fetch updates for repository that has not been opened."); else if (this->commit != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, commit memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, commit memory already allocated."); else if (this->obj != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, object memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, object memory already allocated."); else if (this->ref != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, reference memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, reference memory already allocated."); BOOST_LOG_TRIVIAL(trace) << "Looking up commit referred to by the remote branch \"" << branch << "\"."; this->Call(git_revparse_single(&this->obj, this->repo, (remote + "/" + branch).c_str())); @@ -254,9 +254,9 @@ namespace loot { void GitHelper::CheckoutRevision(const std::string& revision) { if (this->repo == nullptr) - throw Error(Error::git_error, "Cannot checkout revision for repository that has not been opened."); + throw Error(Error::Code::git_error, "Cannot checkout revision for repository that has not been opened."); else if (this->obj != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, object memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, object memory already allocated."); // Get an object ID for 'HEAD^'. this->Call(git_revparse_single(&this->obj, this->repo, revision.c_str())); @@ -275,13 +275,13 @@ namespace loot { std::string GitHelper::GetHeadShortId() { if (this->repo == nullptr) - throw Error(Error::git_error, "Cannot checkout revision for repository that has not been opened."); + throw Error(Error::Code::git_error, "Cannot checkout revision for repository that has not been opened."); else if (this->obj != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, object memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, object memory already allocated."); else if (this->ref != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, reference memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, reference memory already allocated."); else if (this->buf.ptr != nullptr) - throw Error(Error::git_error, "Cannot fetch repository updates, buffer memory already allocated."); + throw Error(Error::Code::git_error, "Cannot fetch repository updates, buffer memory already allocated."); BOOST_LOG_TRIVIAL(trace) << "Getting the Git object for HEAD."; this->Call(git_repository_head(&this->ref, this->repo)); @@ -304,7 +304,7 @@ namespace loot { 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::ok, lc::translate("Unknown: Git repository missing")); + throw Error(Error::Code::ok, lc::translate("Unknown: Git repository missing")); } BOOST_LOG_TRIVIAL(debug) << "Existing repository found, attempting to open it."; diff --git a/src/backend/helpers/helpers.cpp b/src/backend/helpers/helpers.cpp index bc2952b6..f29f28b8 100644 --- a/src/backend/helpers/helpers.cpp +++ b/src/backend/helpers/helpers.cpp @@ -86,7 +86,7 @@ namespace loot { } catch (exception&) { BOOST_LOG_TRIVIAL(error) << "Unable to open \"" << filename.string() << "\" for CRC calculation."; - throw Error(Error::path_read_fail, (boost::format(lc::translate("Unable to open \"%1%\" for CRC calculation.")) % filename.string()).str()); + throw Error(Error::Code::path_read_fail, (boost::format(lc::translate("Unable to open \"%1%\" for CRC calculation.")) % filename.string()).str()); } BOOST_LOG_TRIVIAL(debug) << "CRC32(\"" << filename.string() << "\"): " << std::hex << chksum << std::dec; return chksum; @@ -124,10 +124,10 @@ namespace loot { #ifdef _WIN32 HINSTANCE ret = ShellExecute(0, NULL, ToWinWide(file.string()).c_str(), NULL, NULL, SW_SHOWNORMAL); if ((int)ret <= 32) - throw Error(Error::windows_error, lc::translate("Failed to open file in its default application.")); + throw Error(Error::Code::windows_error, lc::translate("Failed to open file in its default application.")); #else if (system(("/usr/bin/xdg-open" + file.string()).c_str()) != 0) - throw Error(Error::windows_error, lc::translate("Failed to open file in its default application.")); + throw Error(Error::Code::windows_error, lc::translate("Failed to open file in its default application.")); #endif } @@ -149,7 +149,7 @@ namespace loot { else if (keyStr == "HKEY_USERS") hKey = HKEY_USERS; else - throw Error(Error::invalid_args, "Invalid registry key given."); + throw Error(Error::Code::invalid_args, "Invalid registry key given."); BOOST_LOG_TRIVIAL(trace) << "Getting string for registry key, subkey and value: " << keyStr << " + " << subkey << " + " << value; LONG ret = RegGetValue(hKey, diff --git a/src/backend/masterlist.cpp b/src/backend/masterlist.cpp index c09f722f..57ffa6ec 100644 --- a/src/backend/masterlist.cpp +++ b/src/backend/masterlist.cpp @@ -43,11 +43,11 @@ namespace loot { if (!fs::exists(path)) { BOOST_LOG_TRIVIAL(info) << "Unknown masterlist revision: No masterlist present."; - throw Error(Error::ok, lc::translate("N/A: No masterlist present")); + throw Error(Error::Code::ok, lc::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::ok, lc::translate("Unknown: Git repository missing")); + throw Error(Error::Code::ok, lc::translate("Unknown: Git repository missing")); } BOOST_LOG_TRIVIAL(debug) << "Existing repository found, attempting to open it."; @@ -95,7 +95,7 @@ namespace loot { string filename = path.filename().string(); if (repoUrl.empty() || repoBranch.empty()) - throw Error(Error::invalid_args, "Repository URL and branch must not be empty."); + throw Error(Error::Code::invalid_args, "Repository URL and branch must not be empty."); // Initialise checkout options. BOOST_LOG_TRIVIAL(debug) << "Setting up checkout options."; @@ -268,7 +268,7 @@ namespace loot { } while (parsingFailed); if (!parsingError.empty()) - throw Error(Error::ok, parsingError); //Throw an OK because the process still completed in a successful state. + throw Error(Error::Code::ok, parsingError); //Throw an OK because the process still completed in a successful state. return true; } diff --git a/src/backend/metadata/condition_grammar.h b/src/backend/metadata/condition_grammar.h index 7c54e7c7..c4b9d188 100644 --- a/src/backend/metadata/condition_grammar.h +++ b/src/backend/metadata/condition_grammar.h @@ -152,7 +152,7 @@ namespace loot { if (!IsSafePath(file)) { BOOST_LOG_TRIVIAL(error) << "Invalid file path: " << file; - throw loot::Error(loot::Error::invalid_args, boost::locale::translate("Invalid file path:").str() + " " + file); + throw loot::Error(loot::Error::Code::invalid_args, boost::locale::translate("Invalid file path:").str() + " " + file); } if (_game == nullptr) @@ -195,7 +195,7 @@ namespace loot { std::regex(regex, std::regex::ECMAScript | std::regex::icase); } catch (std::regex_error& e) { - throw loot::Error(loot::Error::invalid_args, (boost::format(boost::locale::translate("Invalid regex string \"%1%\": %2%")) % regex % e.what()).str()); + throw loot::Error(loot::Error::Code::invalid_args, (boost::format(boost::locale::translate("Invalid regex string \"%1%\": %2%")) % regex % e.what()).str()); } std::regex sepReg("/|(\\\\\\\\)", std::regex::ECMAScript); @@ -213,7 +213,7 @@ namespace loot { if (!IsSafePath(parent)) { BOOST_LOG_TRIVIAL(error) << "Invalid folder path: " << parent; - throw loot::Error(loot::Error::invalid_args, boost::locale::translate("Invalid folder path:").str() + " " + parent.string()); + throw loot::Error(loot::Error::Code::invalid_args, boost::locale::translate("Invalid folder path:").str() + " " + parent.string()); } std::regex reg; @@ -222,7 +222,7 @@ namespace loot { } catch (std::regex_error& e) { BOOST_LOG_TRIVIAL(error) << "Invalid regex string:" << filename; - throw loot::Error(loot::Error::invalid_args, (boost::format(boost::locale::translate("Invalid regex string \"%1%\": %2%")) % filename % e.what()).str()); + throw loot::Error(loot::Error::Code::invalid_args, (boost::format(boost::locale::translate("Invalid regex string \"%1%\": %2%")) % filename % e.what()).str()); } return std::pair(parent, reg); @@ -290,7 +290,7 @@ namespace loot { if (!IsSafePath(file)) { BOOST_LOG_TRIVIAL(error) << "Invalid file path: " << file; - throw loot::Error(loot::Error::invalid_args, boost::locale::translate("Invalid file path:").str() + " " + file); + throw loot::Error(loot::Error::Code::invalid_args, boost::locale::translate("Invalid file path:").str() + " " + file); } if (_game == nullptr) @@ -378,7 +378,7 @@ namespace loot { void CheckActive(bool& result, const std::string& file) const { if (!IsSafePath(file)) { BOOST_LOG_TRIVIAL(error) << "Invalid file path: " << file; - throw loot::Error(loot::Error::invalid_args, boost::locale::translate("Invalid file path:").str() + " " + file); + throw loot::Error(loot::Error::Code::invalid_args, boost::locale::translate("Invalid file path:").str() + " " + file); } if (_game == nullptr) @@ -398,7 +398,7 @@ namespace loot { BOOST_LOG_TRIVIAL(error) << "Expected \"" << what.tag << "\" at \"" << context << "\"."; - throw loot::Error(loot::Error::condition_eval_fail, (boost::format(boost::locale::translate("Expected \"%1%\" at \"%2%\".")) % what.tag % context).str()); + throw loot::Error(loot::Error::Code::condition_eval_fail, (boost::format(boost::locale::translate("Expected \"%1%\" at \"%2%\".")) % what.tag % context).str()); } //Checks that the path (not regex) doesn't go outside any game folders. diff --git a/src/backend/metadata/conditional_metadata.cpp b/src/backend/metadata/conditional_metadata.cpp index 235ba17a..88c3e59e 100644 --- a/src/backend/metadata/conditional_metadata.cpp +++ b/src/backend/metadata/conditional_metadata.cpp @@ -69,12 +69,12 @@ namespace loot { } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to parse condition \"" << _condition << "\": " << e.what(); - throw loot::Error(loot::Error::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\": %2%")) % _condition % e.what()).str()); + throw loot::Error(loot::Error::Code::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\": %2%")) % _condition % e.what()).str()); } if (!r || begin != end) { BOOST_LOG_TRIVIAL(error) << "Failed to parse condition \"" << _condition << "\"."; - throw loot::Error(loot::Error::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\".")) % _condition).str()); + throw loot::Error(loot::Error::Code::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\".")) % _condition).str()); } game.CacheCondition(_condition, eval); @@ -101,12 +101,12 @@ namespace loot { } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to parse condition \"" << _condition << "\": " << e.what(); - throw loot::Error(loot::Error::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\": %2%")) % _condition % e.what()).str()); + throw loot::Error(loot::Error::Code::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\": %2%")) % _condition % e.what()).str()); } if (!r || begin != end) { BOOST_LOG_TRIVIAL(error) << "Failed to parse condition \"" << _condition << "\"."; - throw loot::Error(loot::Error::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\".")) % _condition).str()); + throw loot::Error(loot::Error::Code::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\".")) % _condition).str()); } } } diff --git a/src/backend/metadata/message.cpp b/src/backend/metadata/message.cpp index ca0d38ce..7e6744d7 100644 --- a/src/backend/metadata/message.cpp +++ b/src/backend/metadata/message.cpp @@ -51,7 +51,7 @@ namespace loot { englishStringExists = true; } if (!englishStringExists) - throw loot::Error(Error::invalid_args, "bad conversion: multilingual messages must contain an English content string"); + throw loot::Error(Error::Code::invalid_args, "bad conversion: multilingual messages must contain an English content string"); } } diff --git a/src/backend/metadata/plugin_metadata.cpp b/src/backend/metadata/plugin_metadata.cpp index 3f6bf8d5..330fe87e 100644 --- a/src/backend/metadata/plugin_metadata.cpp +++ b/src/backend/metadata/plugin_metadata.cpp @@ -277,7 +277,7 @@ namespace loot { void PluginMetadata::Priority(const int p) { if (abs(p) >= yamlGlobalPriorityDivisor) - throw Error(Error::invalid_args, "Cannot set priority that has an absolute value greater than or equal to " + to_string(yamlGlobalPriorityDivisor)); + throw Error(Error::Code::invalid_args, "Cannot set priority that has an absolute value greater than or equal to " + to_string(yamlGlobalPriorityDivisor)); priority = p; } diff --git a/src/backend/metadata_list.cpp b/src/backend/metadata_list.cpp index bac5ad5d..d90ce78f 100644 --- a/src/backend/metadata_list.cpp +++ b/src/backend/metadata_list.cpp @@ -39,7 +39,7 @@ namespace loot { boost::filesystem::ifstream in(filepath); if (!in.good()) - throw Error(Error::path_read_fail, "Cannot open " + filepath.string()); + throw Error(Error::Code::path_read_fail, "Cannot open " + filepath.string()); YAML::Node metadataList = YAML::Load(in); in.close(); @@ -51,7 +51,7 @@ namespace loot { regexPlugins.push_back(plugin); else { if (!plugins.insert(plugin).second) - throw Error(Error::path_read_fail, "More than one entry exists for \"" + plugin.Name() + "\""); + throw Error(Error::Code::path_read_fail, "More than one entry exists for \"" + plugin.Name() + "\""); } } } @@ -127,7 +127,7 @@ namespace loot { regexPlugins.push_back(plugin); else { if (!plugins.insert(plugin).second) - throw Error(Error::invalid_args, "Cannot add \"" + plugin.Name() + "\" to the metadata list as another entry already exists."); + throw Error(Error::Code::invalid_args, "Cannot add \"" + plugin.Name() + "\" to the metadata list as another entry already exists."); } } diff --git a/src/backend/plugin/plugin_sorter.cpp b/src/backend/plugin/plugin_sorter.cpp index a5634b5b..a3fda771 100644 --- a/src/backend/plugin/plugin_sorter.cpp +++ b/src/backend/plugin/plugin_sorter.cpp @@ -76,7 +76,7 @@ namespace loot { BOOST_LOG_TRIVIAL(error) << "Cyclic interaction detected between plugins \"" << graph[source].Name() << "\" and \"" << graph[target].Name() << "\". Back cycle: " << backCycle; - throw loot::Error(loot::Error::sorting_error, (boost::format(boost::locale::translate("Cyclic interaction detected between plugins \"%1%\" and \"%2%\". Back cycle: %3%")) % graph[source].Name() % graph[target].Name() % backCycle).str()); + throw loot::Error(loot::Error::Code::sorting_error, (boost::format(boost::locale::translate("Cyclic interaction detected between plugins \"%1%\" and \"%2%\". Back cycle: %3%")) % graph[source].Name() % graph[target].Name() % backCycle).str()); } private: @@ -89,7 +89,7 @@ namespace loot { inline void discover_vertex(vertex_t vertex, const PluginGraph& graph) { if (vertex == target) - throw Error(Error::ok, "Found a path."); + throw Error(Error::Code::ok, "Found a path."); } private: @@ -247,7 +247,7 @@ namespace loot { boost::breadth_first_search(graph, toVertex, visitor(PathDetector(fromVertex)).vertex_index_map(vertexIndexMap)); } catch (Error& e) { - if (e.code() == Error::ok) + if (e.code() == Error::Code::ok) return true; } return false; diff --git a/src/gui/query_handler.cpp b/src/gui/query_handler.cpp index 07354e6a..44ce348a 100644 --- a/src/gui/query_handler.cpp +++ b/src/gui/query_handler.cpp @@ -73,7 +73,7 @@ namespace loot { } catch (Error &e) { BOOST_LOG_TRIVIAL(error) << e.what(); - callback->Failure(e.code(), e.what()); + callback->Failure(e.codeAsUnsignedInt(), e.what()); } catch (exception &e) { BOOST_LOG_TRIVIAL(error) << e.what(); @@ -88,7 +88,7 @@ namespace loot { } catch (Error &e) { BOOST_LOG_TRIVIAL(error) << e.what(); - callback->Failure(e.code(), e.what()); + callback->Failure(e.codeAsUnsignedInt(), e.what()); } catch (exception &e) { BOOST_LOG_TRIVIAL(error) << e.what(); @@ -137,7 +137,7 @@ namespace loot { } catch (Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to redate plugins. " << e.what(); - callback->Failure(e.code(), e.what()); + callback->Failure(e.codeAsUnsignedInt(), e.what()); } catch (exception &e) { BOOST_LOG_TRIVIAL(error) << "Failed to redate plugins. " << e.what(); @@ -224,7 +224,7 @@ namespace loot { } catch (loot::Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to change game. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to change game. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to change game. Details: %1%")) % e.what()).str()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to change game. Details: " << e.what(); @@ -245,7 +245,7 @@ namespace loot { } catch (loot::Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to copy plugin metadata. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to copy plugin metadata. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to copy plugin metadata. Details: %1%")) % e.what()).str()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to copy plugin metadata. Details: " << e.what(); @@ -267,7 +267,7 @@ namespace loot { } catch (loot::Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to apply plugin metadata. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to apply plugin metadata. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to apply plugin metadata. Details: %1%")) % e.what()).str()); } catch (std::exception& e) { // If this was a YAML conversion error, cut off the line and column numbers, @@ -311,7 +311,7 @@ namespace loot { } catch (Error &e) { BOOST_LOG_TRIVIAL(error) << e.what(); - callback->Failure(e.code(), e.what()); + callback->Failure(e.codeAsUnsignedInt(), e.what()); } catch (exception &e) { BOOST_LOG_TRIVIAL(error) << e.what(); @@ -335,7 +335,7 @@ namespace loot { } catch (loot::Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to copy plugin metadata. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to copy plugin metadata. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to copy plugin metadata. Details: %1%")) % e.what()).str()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to copy plugin metadata. Details: " << e.what(); @@ -371,7 +371,7 @@ namespace loot { } catch (loot::Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to copy plugin metadata. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to copy plugin metadata. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to copy plugin metadata. Details: %1%")) % e.what()).str()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to copy plugin metadata. Details: " << e.what(); @@ -772,7 +772,7 @@ namespace loot { } catch (loot::Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to get game data. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to get game data. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to get game data. Details: %1%")) % e.what()).str()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to get game data. Details: " << e.what(); @@ -789,7 +789,7 @@ namespace loot { wasChanged = _lootState.CurrentGame().GetMasterlist().Update(_lootState.CurrentGame()); } catch (loot::Error &e) { - if (e.code() == loot::Error::ok) { + if (e.code() == loot::Error::Code::ok) { // There was a parsing error, but roll-back was successful, so the process // should still complete. @@ -863,7 +863,7 @@ namespace loot { } catch (Error &e) { BOOST_LOG_TRIVIAL(error) << "Failed to update the masterlist. Details: " << e.what(); - callback->Failure(e.code(), (boost::format(loc::translate("Failed to update the masterlist. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to update the masterlist. Details: %1%")) % e.what()).str()); } catch (exception &e) { BOOST_LOG_TRIVIAL(error) << "Failed to update the masterlist. Details: " << e.what(); @@ -961,7 +961,7 @@ namespace loot { } catch (loot::Error& e) { BOOST_LOG_TRIVIAL(error) << "Failed to sort plugins. Details: " << e.what(); - if (e.code() == Error::sorting_error) { + if (e.code() == Error::Code::sorting_error) { _lootState.CurrentGame().AppendMessage(Message(Message::error, e.what())); YAML::Node node; @@ -969,7 +969,7 @@ namespace loot { callback->Success(JSON::stringify(node)); } else - callback->Failure(e.code(), (boost::format(loc::translate("Failed to sort plugins. Details: %1%")) % e.what()).str()); + callback->Failure(e.codeAsUnsignedInt(), (boost::format(loc::translate("Failed to sort plugins. Details: %1%")) % e.what()).str()); } } @@ -1059,11 +1059,11 @@ namespace loot { void QueryHandler::CopyToClipboard(const std::string& text) { #ifdef _WIN32 if (!OpenClipboard(NULL)) { - throw loot::Error(loot::Error::windows_error, "Failed to open the Windows clipboard."); + throw loot::Error(loot::Error::Code::windows_error, "Failed to open the Windows clipboard."); } if (!EmptyClipboard()) { - throw loot::Error(loot::Error::windows_error, "Failed to empty the Windows clipboard."); + throw loot::Error(loot::Error::Code::windows_error, "Failed to empty the Windows clipboard."); } // The clipboard takes a Unicode (ie. UTF-16) string that it then owns and must not @@ -1074,11 +1074,11 @@ namespace loot { wcscpy(wcstr, wtext.c_str()); if (SetClipboardData(CF_UNICODETEXT, wcstr) == NULL) { - throw loot::Error(loot::Error::windows_error, "Failed to copy metadata to the Windows clipboard."); + throw loot::Error(loot::Error::Code::windows_error, "Failed to copy metadata to the Windows clipboard."); } if (!CloseClipboard()) { - throw loot::Error(loot::Error::windows_error, "Failed to close the Windows clipboard."); + throw loot::Error(loot::Error::Code::windows_error, "Failed to close the Windows clipboard."); } #endif }