Add error handling in serialize() & deserialize()
Refined the serialize and deserialize functions to incorporate robust
error handling. These checks would safeguard the application against
potential runtime crashes.
Change-Id: I39923feeb489a5270b961278118abcc7f4c31dee
Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
diff --git a/src/manager_serialize.cpp b/src/manager_serialize.cpp
index c0a2782..57b8991 100644
--- a/src/manager_serialize.cpp
+++ b/src/manager_serialize.cpp
@@ -55,9 +55,24 @@
void serialize(const Manager& obj, const fs::path& path)
{
- std::ofstream os(path.c_str(), std::ios::out | std::ios::binary);
- cereal::BinaryOutputArchive oarchive(os);
- oarchive(obj);
+ try
+ {
+ std::ofstream os(path, std::ios::out | std::ios::binary);
+
+ if (!os.is_open())
+ {
+ lg2::error("Failed to open file for serialization: {FILE}", "FILE",
+ path);
+ return;
+ }
+
+ cereal::BinaryOutputArchive oarchive(os);
+ oarchive(obj);
+ }
+ catch (const std::exception& e)
+ {
+ lg2::error("Failed to Serialize : {ERROR} ", "ERROR", e);
+ }
}
bool deserialize(const fs::path& path, Manager& entry)
@@ -67,6 +82,12 @@
if (fs::exists(path))
{
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+ if (!is.is_open())
+ {
+ lg2::error("Failed to open file for deserialization: {FILE}",
+ "FILE", path);
+ return false;
+ }
cereal::BinaryInputArchive iarchive(is);
iarchive(entry);
return true;