From eabe3082868396520b6a63466a2c0a0c3f755a3e Mon Sep 17 00:00:00 2001 From: David Pires <79417054+Dacops@users.noreply.github.com> Date: Thu, 18 Jan 2024 18:38:26 +0000 Subject: [PATCH] feat(engine): add missing error handling in json bridge --- .../cubos/engine/assets/bridges/json.hpp | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/engine/include/cubos/engine/assets/bridges/json.hpp b/engine/include/cubos/engine/assets/bridges/json.hpp index be67d97ce3..6c047d1bd8 100644 --- a/engine/include/cubos/engine/assets/bridges/json.hpp +++ b/engine/include/cubos/engine/assets/bridges/json.hpp @@ -39,13 +39,27 @@ namespace cubos::engine stream.readUntil(jsonStr, nullptr); core::data::JSONDeserializer deserializer{}; - // New JSONDeserializer() receives a JSON object to deserialize from - nlohmann::json json = nlohmann::json::parse(jsonStr); + // JSONDeserializer() receives a JSON object to deserialize from + nlohmann::json json{}; + + try + { + json = nlohmann::json::parse(jsonStr); + } + catch (nlohmann::json::parse_error& e) + { + CUBOS_ERROR("{}", e.what()); + return false; + } deserializer.feed(json); // Deserialize the asset and store it in the asset manager. T data{}; - deserializer.read(data); + if (deserializer.read(data)) + { + CUBOS_ERROR("Could not deserialize asset from JSON file"); + return false; + } assets.store(handle, std::move(data)); return true; @@ -58,7 +72,11 @@ namespace cubos::engine // Read the asset from the asset manager and serialize it to the file stream. auto data = assets.read(handle); - serializer.write(*data); + if (serializer.write(*data)) + { + CUBOS_ERROR("Could not serialize asset to JSON file"); + return false; + } // new JSONSerializer() does not receive a stream to write to, need to write to it manually auto jsonStr = serializer.output().dump();