From a82dae1172f4482963a16be43f5bb33b24781ca7 Mon Sep 17 00:00:00 2001 From: WrinklyNinja Date: Wed, 14 May 2014 21:41:37 +0100 Subject: [PATCH] Improved exception handling. Should fix #146. --- src/backend/metadata.cpp | 6 +++--- src/backend/network.cpp | 6 +++--- src/gui/main.cpp | 35 +++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/backend/metadata.cpp b/src/backend/metadata.cpp index c18c9a6e..7afe1774 100644 --- a/src/backend/metadata.cpp +++ b/src/backend/metadata.cpp @@ -139,14 +139,14 @@ namespace loot { bool r; try { r = boost::spirit::qi::phrase_parse(begin, end, grammar, skipper, eval); - } catch (loot::error& e) { + } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to parse condition \"" << _condition << "\": " << e.what(); - throw loot::error(loot::error::path_read_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\": %2%")) % _condition % e.what()).str()); + throw loot::error(loot::error::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::path_read_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\".")) % _condition).str()); + throw loot::error(loot::error::condition_eval_fail, (boost::format(lc::translate("Failed to parse condition \"%1%\".")) % _condition).str()); } game.conditionCache.emplace(boost::to_lower_copy(_condition), eval); diff --git a/src/backend/network.cpp b/src/backend/network.cpp index a4839bea..ed1e38d2 100644 --- a/src/backend/network.cpp +++ b/src/backend/network.cpp @@ -60,12 +60,12 @@ namespace loot { if (!error_code) return; - const git_error * error = giterr_last(); + const git_error * last_error = giterr_last(); std::string error_message; - if (error == NULL) + if (last_error == NULL) error_message = IntToString(error_code) + "."; else - error_message = IntToString(error_code) + "; " + error->message; + error_message = IntToString(error_code) + "; " + last_error->message; free(); giterr_clear(); diff --git a/src/gui/main.cpp b/src/gui/main.cpp index d1c096eb..8ffc25d2 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -113,7 +113,7 @@ struct masterlist_updater_parser { pair ret = UpdateMasterlist(_game, _errors, _plugins, _messages); _revision = ret.first; _date = ret.second; - } catch (loot::error& e) { + } catch (std::exception& e) { _plugins.clear(); _messages.clear(); BOOST_LOG_TRIVIAL(error) << "Masterlist update failed. Details: " << e.what(); @@ -124,7 +124,7 @@ struct masterlist_updater_parser { _revision = ret.first; _date = ret.second; } - catch (loot::error& e) { + catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Masterlist revision check failed. Details: " << e.what(); _errors.push_back(loot::Message(loot::Message::error, (format(loc::translate("Masterlist revision check failed. Details: %1%")) % e.what()).str())); } @@ -137,7 +137,7 @@ struct masterlist_updater_parser { _revision = ret.first; _date = ret.second; } - catch (loot::error& e) { + catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Masterlist revision check failed. Details: " << e.what(); _errors.push_back(loot::Message(loot::Message::error, (format(loc::translate("Masterlist revision check failed. Details: %1%")) % e.what()).str())); } @@ -328,14 +328,6 @@ bool LOOT::OnInit() { BOOST_LOG_TRIVIAL(debug) << "Detecting installed games."; try { _games = GetGames(_settings); - } catch (loot::error& e) { - BOOST_LOG_TRIVIAL(error) << "Game-specific settings could not be initialised. " << e.what(); - wxMessageBox( - FromUTF8(format(loc::translate("Error: Game-specific settings could not be initialised. %1%")) % e.what()), - translate("LOOT: Error"), - wxOK | wxICON_ERROR, - NULL); - return false; } catch (YAML::Exception& e) { BOOST_LOG_TRIVIAL(error) << "Games' settings parsing failed. " << e.what(); wxMessageBox( @@ -345,6 +337,15 @@ bool LOOT::OnInit() { NULL); return false; } + catch (std::exception& e) { + BOOST_LOG_TRIVIAL(error) << "Game-specific settings could not be initialised. " << e.what(); + wxMessageBox( + FromUTF8(format(loc::translate("Error: Game-specific settings could not be initialised. %1%")) % e.what()), + translate("LOOT: Error"), + wxOK | wxICON_ERROR, + NULL); + return false; + } BOOST_LOG_TRIVIAL(debug) << "Selecting game."; string target; @@ -386,7 +387,7 @@ bool LOOT::OnInit() { try { _game.Init(); *find(_games.begin(), _games.end(), _game) = _game; //Sync changes. - } catch (loot::error& e) { + } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Game-specific settings could not be initialised. " << e.what(); wxMessageBox( FromUTF8(format(loc::translate("Error: Game-specific settings could not be initialised. %1%")) % e.what()), @@ -549,7 +550,7 @@ void Launcher::OnGameChange(wxCommandEvent& event) { _game->Init(); //In case it hasn't already been done. BOOST_LOG_TRIVIAL(debug) << "New game is " << _game->Name(); } - catch (loot::error& e) { + catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Game-specific settings could not be initialised." << e.what(); wxMessageBox( FromUTF8(format(loc::translate("Error: Game-specific settings could not be initialised. %1%")) % e.what()), @@ -728,7 +729,8 @@ void Launcher::OnSortPlugins(wxCommandEvent& event) { else ++it; } - } catch (loot::error& e) { + } + catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "A global message contains a condition that could not be evaluated. Details: " << e.what(); messages.push_back(loot::Message(loot::Message::error, (format(loc::translate("A global message contains a condition that could not be evaluated. Details: %1%")) % e.what()).str())); } @@ -761,7 +763,8 @@ void Launcher::OnSortPlugins(wxCommandEvent& event) { BOOST_LOG_TRIVIAL(trace) << "Evaluate conditions for merged plugin data."; try { graph[*vit].EvalAllConditions(*_game, lang); - } catch (loot::error& e) { + } + catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "\"" << graph[*vit].Name() << "\" contains a condition that could not be evaluated. Details: " << e.what(); messages.push_back(loot::Message(loot::Message::error, (format(loc::translate("\"%1%\" contains a condition that could not be evaluated. Details: %2%")) % graph[*vit].Name() % e.what()).str())); } @@ -890,7 +893,7 @@ void Launcher::OnSortPlugins(wxCommandEvent& event) { try { _game->SetLoadOrder(plugins); } - catch (loot::error& e) { + catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Failed to set the load order. Details: " << e.what(); messages.push_back(loot::Message(loot::Message::error, (format(loc::translate("Failed to set the load order. Details: %1%")) % e.what()).str())); }