From de2ba9ce7ffb2e5fe32d88d86e3a7c8865ebbec9 Mon Sep 17 00:00:00 2001 From: William JCM Date: Sat, 26 Feb 2022 14:48:45 +0100 Subject: [PATCH] Mass(Manager),SaveTool: improve error handling. --- src/Mass/Mass.cpp | 10 +++++-- src/MassManager/MassManager.cpp | 36 +++++++++++++----------- src/SaveTool/SaveTool_MainManager.cpp | 17 ++++------- src/SaveTool/SaveTool_ProfileManager.cpp | 9 ++---- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/Mass/Mass.cpp b/src/Mass/Mass.cpp index 7b81c68..ff8b6cd 100644 --- a/src/Mass/Mass.cpp +++ b/src/Mass/Mass.cpp @@ -14,8 +14,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#include - #include #include @@ -914,12 +912,18 @@ auto Mass::updateAccount(const std::string& new_account) -> bool { auto account = _mass->at("Account"); if(!account) { _state = State::Invalid; + _lastError = "Couldn't find the account property."; return false; } account->value = new_account; - return _mass->saveToFile(); + if(!_mass->saveToFile()) { + _lastError = _mass->lastError(); + return false; + } + + return true; } void Mass::getCustomStyles(Containers::ArrayView styles, ArrayProperty* style_array) { diff --git a/src/MassManager/MassManager.cpp b/src/MassManager/MassManager.cpp index d6338c3..22b18bf 100644 --- a/src/MassManager/MassManager.cpp +++ b/src/MassManager/MassManager.cpp @@ -49,11 +49,11 @@ auto MassManager::lastError() -> std::string const& { return _lastError; } -auto MassManager::hangar(int hangar) -> Mass& { +auto MassManager::hangar(Int hangar) -> Mass& { return _hangars[hangar]; } -void MassManager::refreshHangar(int hangar) { +void MassManager::refreshHangar(Int hangar) { if(hangar < 0 || hangar >= 32) { return; } @@ -63,7 +63,7 @@ void MassManager::refreshHangar(int hangar) { _hangars[hangar] = Mass{mass_filename}; } -auto MassManager::importMass(const std::string& staged_fn, int hangar) -> bool { +auto MassManager::importMass(const std::string& staged_fn, Int hangar) -> bool { if(hangar < 0 || hangar >= 32) { _lastError = "Hangar out of range in MassManager::importMass()"; return false; @@ -79,18 +79,22 @@ auto MassManager::importMass(const std::string& staged_fn, int hangar) -> bool { std::string source = Utility::Directory::join(_stagingAreaDirectory, staged_fn); Utility::Directory::copy(source, source + ".tmp"); - if(!Mass{source + ".tmp"}.updateAccount(_steamId)) { - _lastError = "The M.A.S.S. file at " + source + " seems to be corrupt."; - Utility::Directory::rm(source + ".tmp"); - return false; + Mass mass{source + ".tmp"}; + if(!mass.updateAccount(_steamId)) { + _lastError = mass.lastError(); + Utility::Directory::rm(source + ".tmp"); + return false; + } } - if(Utility::Directory::exists(Utility::Directory::join(_saveDirectory, _hangars[hangar].filename()))) { - Utility::Directory::rm(Utility::Directory::join(_saveDirectory, _hangars[hangar].filename())); + std::string dest = Utility::Directory::join(_saveDirectory, _hangars[hangar].filename()); + + if(Utility::Directory::exists(dest)) { + Utility::Directory::rm(dest); } - if(!Utility::Directory::move(source + ".tmp", Utility::Directory::join(_saveDirectory, _hangars[hangar].filename()))) { + if(!Utility::Directory::move(source + ".tmp", dest)) { _lastError = Utility::formatString("Couldn't move {} to hangar {:.2d}", staged_fn, hangar + 1); return false; } @@ -98,7 +102,7 @@ auto MassManager::importMass(const std::string& staged_fn, int hangar) -> bool { return true; } -auto MassManager::exportMass(int hangar) -> bool { +auto MassManager::exportMass(Int hangar) -> bool { if(hangar < 0 || hangar >= 32) { _lastError = "Hangar out of range in MassManager::exportMass()"; return false; @@ -121,7 +125,7 @@ auto MassManager::exportMass(int hangar) -> bool { return true; } -auto MassManager::moveMass(int source, int destination) -> bool { +auto MassManager::moveMass(Int source, Int destination) -> bool { if(source < 0 || source >= 32) { _lastError = "Source hangar out of range."; return false; @@ -156,14 +160,14 @@ auto MassManager::moveMass(int source, int destination) -> bool { return true; } -auto MassManager::deleteMass(int hangar) -> bool { +auto MassManager::deleteMass(Int hangar) -> bool { if(hangar < 0 || hangar >= 32) { - _lastError = "Hangar out of bounds"; + _lastError = "Hangar out of range."; return false; } if(!Utility::Directory::rm(Utility::Directory::join(_saveDirectory, _hangars[hangar].filename()))) { - _lastError = "Deletion failed. Maybe the file was already deleted, or it's locked by another application."; + _lastError = Utility::formatString("Deletion failed: {}", std::strerror(errno)); return false; } @@ -202,7 +206,7 @@ auto MassManager::deleteStagedMass(const std::string& filename) -> bool { } if(!Utility::Directory::rm(Utility::Directory::join(_stagingAreaDirectory, filename))) { - _lastError = "The file " + filename + " couldn't be deleted for unknown reasons."; + _lastError = Utility::formatString("{} couldn't be deleted: {}", filename, std::strerror(errno)); return false; } diff --git a/src/SaveTool/SaveTool_MainManager.cpp b/src/SaveTool/SaveTool_MainManager.cpp index 8dc5e2f..fdb53a0 100644 --- a/src/SaveTool/SaveTool_MainManager.cpp +++ b/src/SaveTool/SaveTool_MainManager.cpp @@ -445,8 +445,7 @@ void SaveTool::drawMassManager() { std::string file = *(static_cast(payload->Data)); if(!_massManager->importMass(file, i)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error importing M.A.S.S.", - _massManager->lastError().c_str(), window()); + _queue.addToast(Toast::Type::Error, _massManager->lastError()); } } else if((payload = ImGui::AcceptDragDropPayload("Mass"))) { @@ -460,9 +459,7 @@ void SaveTool::drawMassManager() { int index = *(static_cast(payload->Data)); if(!_massManager->moveMass(index, i)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", - _massManager->lastError().c_str(), - window()); + _queue.addToast(Toast::Type::Error, _massManager->lastError()); } } @@ -575,9 +572,7 @@ void SaveTool::drawMassManager() { int index = *(static_cast(payload->Data)); if(!_massManager->exportMass(index)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", - _massManager->lastError().c_str(), - window()); + _queue.addToast(Toast::Type::Error, _massManager->lastError()); } } @@ -626,8 +621,7 @@ auto SaveTool::drawDeleteMassPopup(int mass_index) -> ImGuiID { ImGui::TableSetColumnIndex(1); if(ImGui::Button("Yes")) { if(!_massManager->deleteMass(mass_index)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error when deleting M.A.S.S.", - _massManager->lastError().c_str(), window()); + _queue.addToast(Toast::Type::Error, _massManager->lastError()); } ImGui::CloseCurrentPopup(); } @@ -665,8 +659,7 @@ auto SaveTool::drawDeleteStagedMassPopup(const std::string& filename) -> ImGuiID ImGui::TableSetColumnIndex(1); if(ImGui::Button("Yes")) { if(!_massManager->deleteStagedMass(filename)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error when deleting M.A.S.S.", - _massManager->lastError().c_str(), window()); + _queue.addToast(Toast::Type::Error, _massManager->lastError()); } ImGui::CloseCurrentPopup(); } diff --git a/src/SaveTool/SaveTool_ProfileManager.cpp b/src/SaveTool/SaveTool_ProfileManager.cpp index 07656c7..d501d1b 100644 --- a/src/SaveTool/SaveTool_ProfileManager.cpp +++ b/src/SaveTool/SaveTool_ProfileManager.cpp @@ -150,8 +150,7 @@ auto SaveTool::drawBackupListPopup() -> ImGuiID { ImGui::TableSetColumnIndex(1); if(ImGui::Button("Yes")) { if(!_profileManager->restoreBackup(backup_index)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error when restoring backup", - _profileManager->lastError().c_str(), window()); + _queue.addToast(Toast::Type::Error, _profileManager->lastError()); } _profileManager->refreshProfiles(); ImGui::CloseCurrentPopup(); @@ -190,8 +189,7 @@ auto SaveTool::drawBackupListPopup() -> ImGuiID { ImGui::TableSetColumnIndex(1); if(ImGui::Button("Yes")) { if(!_profileManager->deleteBackup(backup_index)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error when deleting backup", - _profileManager->lastError().c_str(), window()); + _queue.addToast(Toast::Type::Error, _profileManager->lastError()); } ImGui::CloseCurrentPopup(); } @@ -378,8 +376,7 @@ auto SaveTool::drawDeleteProfilePopup(std::size_t profile_index) -> ImGuiID { ImGui::TableSetColumnIndex(1); if(ImGui::Button("Yes")) { if(!_profileManager->deleteProfile(profile_index, delete_builds)) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error when deleting profile", - _profileManager->lastError().c_str(), window()); + _queue.addToast(Toast::Type::Error, _profileManager->lastError()); } ImGui::CloseCurrentPopup(); }