Mass(Manager),SaveTool: improve error handling.

This commit is contained in:
Guillaume Jacquemin 2022-02-26 14:48:45 +01:00
parent d0ddc73852
commit de2ba9ce7f
4 changed files with 35 additions and 37 deletions

View File

@ -14,8 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstring>
#include <algorithm>
#include <Corrade/Containers/Array.h>
@ -914,12 +912,18 @@ auto Mass::updateAccount(const std::string& new_account) -> bool {
auto account = _mass->at<StringProperty>("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<CustomStyle> styles, ArrayProperty* style_array) {

View File

@ -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;
}

View File

@ -445,8 +445,7 @@ void SaveTool::drawMassManager() {
std::string file = *(static_cast<std::string*>(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<int*>(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<int*>(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();
}

View File

@ -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();
}