From ebdd3914f4fa5403725c00ac0db2ab9f67660dc5 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 3 Mar 2022 19:13:43 +0100 Subject: [PATCH] added try catch for more safety --- mission/memory/NVMParameterBase.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mission/memory/NVMParameterBase.cpp b/mission/memory/NVMParameterBase.cpp index c7ac7560..78f40525 100644 --- a/mission/memory/NVMParameterBase.cpp +++ b/mission/memory/NVMParameterBase.cpp @@ -11,7 +11,12 @@ ReturnValue_t NVMParameterBase::readJsonFile() { if (std::filesystem::exists(fullName)) { // Read JSON file content into object std::ifstream i(fullName); - i >> json; + try { + i >> json; + } catch (nlohmann::json::exception& e) { + sif::warning << "Reading JSON file failed with error " << e.what() << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } return HasReturnvaluesIF::RETURN_OK; } return HasFileSystemIF::FILE_DOES_NOT_EXIST; @@ -19,7 +24,12 @@ ReturnValue_t NVMParameterBase::readJsonFile() { ReturnValue_t NVMParameterBase::writeJsonFile() { std::ofstream o(fullName); - o << std::setw(4) << json << std::endl; + try { + o << std::setw(4) << json << std::endl; + } catch (nlohmann::json::exception& e) { + sif::warning << "Writing JSON file failed with error " << e.what() << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } return HasReturnvaluesIF::RETURN_OK; } -- 2.43.0