From 3bd4a3ba8b718fc465195e59f080467eef61ab0b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 16 May 2023 11:33:00 +0200 Subject: [PATCH] impr: Better JSON error handling in many places --- .../builtin/source/content/background_services.cpp | 6 +++--- .../source/content/data_processor_nodes.cpp | 13 +++++++------ plugins/builtin/source/content/hashes.cpp | 10 +++++----- plugins/builtin/source/content/providers.cpp | 6 +++--- .../source/content/providers/disk_provider.cpp | 2 +- .../source/content/providers/file_provider.cpp | 2 +- .../source/content/providers/gdb_provider.cpp | 6 +++--- .../content/providers/intel_hex_provider.cpp | 2 +- plugins/builtin/source/content/tools_entries.cpp | 8 ++++---- .../source/content/views/view_constants.cpp | 12 ++++++------ .../source/content/views/view_data_processor.cpp | 14 +++++++------- .../builtin/source/content/views/view_hashes.cpp | 2 +- .../builtin/source/content/views/view_patches.cpp | 2 +- plugins/builtin/source/content/welcome_screen.cpp | 6 +++--- 14 files changed, 46 insertions(+), 45 deletions(-) diff --git a/plugins/builtin/source/content/background_services.cpp b/plugins/builtin/source/content/background_services.cpp index 44cd96e79..162e518cd 100644 --- a/plugins/builtin/source/content/background_services.cpp +++ b/plugins/builtin/source/content/background_services.cpp @@ -27,10 +27,10 @@ namespace hex::plugin::builtin { auto json = nlohmann::json::parse(data.begin(), data.end()); const auto &endpoints = ContentRegistry::CommunicationInterface::impl::getNetworkEndpoints(); - if (auto callback = endpoints.find(json["endpoint"].get()); callback != endpoints.end()) { - log::info("Network endpoint {} called with arguments '{}'", json["endpoint"].get(), json.contains("data") ? json["data"].dump() : ""); + if (auto callback = endpoints.find(json.at("endpoint").get()); callback != endpoints.end()) { + log::info("Network endpoint {} called with arguments '{}'", json.at("endpoint").get(), json.contains("data") ? json.at("data").dump() : ""); - auto responseData = callback->second(json.contains("data") ? json["data"] : nlohmann::json::object()); + auto responseData = callback->second(json.contains("data") ? json.at("data") : nlohmann::json::object()); result["status"] = "success"; result["data"] = responseData; diff --git a/plugins/builtin/source/content/data_processor_nodes.cpp b/plugins/builtin/source/content/data_processor_nodes.cpp index 349a04ebd..787d32636 100644 --- a/plugins/builtin/source/content/data_processor_nodes.cpp +++ b/plugins/builtin/source/content/data_processor_nodes.cpp @@ -59,8 +59,8 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_size = j["size"]; - this->m_buffer = j["data"].get>(); + this->m_size = j.at("size"); + this->m_buffer = j.at("data").get>(); } private: @@ -89,7 +89,7 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_value = j["data"].get(); + this->m_value = j.at("data").get(); } private: @@ -117,7 +117,7 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_value = j["data"]; + this->m_value = j.at("data"); } private: @@ -145,7 +145,7 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_value = j["data"]; + this->m_value = j.at("data"); } private: @@ -184,7 +184,8 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_color = ImVec4(j["data"]["r"], j["data"]["g"], j["data"]["b"], j["data"]["a"]); + const auto &color = j.at("data"); + this->m_color = ImVec4(color.at("r"), color.at("g"), color.at("b"), color.at("a")); } private: diff --git a/plugins/builtin/source/content/hashes.cpp b/plugins/builtin/source/content/hashes.cpp index 533fbd294..ad163a73f 100644 --- a/plugins/builtin/source/content/hashes.cpp +++ b/plugins/builtin/source/content/hashes.cpp @@ -147,11 +147,11 @@ namespace hex::plugin::builtin { void load(const nlohmann::json &json) override { try { - this->m_polynomial = json["polynomial"]; - this->m_initialValue = json["initialValue"]; - this->m_xorOut = json["xorOut"]; - this->m_reflectIn = json["reflectIn"]; - this->m_reflectOut = json["reflectOut"]; + this->m_polynomial = json.at("polynomial"); + this->m_initialValue = json.at("initialValue"); + this->m_xorOut = json.at("xorOut"); + this->m_reflectIn = json.at("reflectIn"); + this->m_reflectOut = json.at("reflectOut"); } catch (std::exception&) { } } diff --git a/plugins/builtin/source/content/providers.cpp b/plugins/builtin/source/content/providers.cpp index d07d7307c..a6c218d6b 100644 --- a/plugins/builtin/source/content/providers.cpp +++ b/plugins/builtin/source/content/providers.cpp @@ -35,13 +35,13 @@ namespace hex::plugin::builtin { .required = true, .load = [](const std::fs::path &basePath, Tar &tar) { auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json")); - auto providerIds = json["providers"].get>(); + auto providerIds = json.at("providers").get>(); bool success = true; for (const auto &id : providerIds) { auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id))); - auto provider = ImHexApi::Provider::createProvider(providerSettings["type"].get(), true); + auto provider = ImHexApi::Provider::createProvider(providerSettings.at("type").get(), true); ON_SCOPE_EXIT { if (!success) { for (auto &task : TaskManager::getRunningTasks()) @@ -60,7 +60,7 @@ namespace hex::plugin::builtin { } provider->setID(id); - provider->loadSettings(providerSettings["settings"]); + provider->loadSettings(providerSettings.at("settings")); if (!provider->open() || !provider->isAvailable() || !provider->isReadable()) success = false; else diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 1d9230ced..221cc0a01 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -359,7 +359,7 @@ namespace hex::plugin::builtin { void DiskProvider::loadSettings(const nlohmann::json &settings) { Provider::loadSettings(settings); - auto path = settings["path"].get(); + auto path = settings.at("path").get(); this->setPath(std::u8string(path.begin(), path.end())); this->reloadDrives(); } diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 384386382..d01d2f248 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -217,7 +217,7 @@ namespace hex::plugin::builtin { void FileProvider::loadSettings(const nlohmann::json &settings) { Provider::loadSettings(settings); - auto pathString = settings["path"].get(); + auto pathString = settings.at("path").get(); std::fs::path path = std::u8string(pathString.begin(), pathString.end()); if (auto projectPath = ProjectFile::getPath(); !projectPath.empty()) diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index e1afc1bcd..2f5e2ba30 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -325,9 +325,9 @@ namespace hex::plugin::builtin { void GDBProvider::loadSettings(const nlohmann::json &settings) { Provider::loadSettings(settings); - this->m_ipAddress = settings["ip"].get(); - this->m_port = settings["port"].get(); - this->m_size = settings["size"].get(); + this->m_ipAddress = settings.at("ip").get(); + this->m_port = settings.at("port").get(); + this->m_size = settings.at("size").get(); } nlohmann::json GDBProvider::storeSettings(nlohmann::json settings) const { diff --git a/plugins/builtin/source/content/providers/intel_hex_provider.cpp b/plugins/builtin/source/content/providers/intel_hex_provider.cpp index dbd8f9ba6..8ceeb8f09 100644 --- a/plugins/builtin/source/content/providers/intel_hex_provider.cpp +++ b/plugins/builtin/source/content/providers/intel_hex_provider.cpp @@ -269,7 +269,7 @@ namespace hex::plugin::builtin { void IntelHexProvider::loadSettings(const nlohmann::json &settings) { Provider::loadSettings(settings); - auto path = settings["path"].get(); + auto path = settings.at("path").get(); this->m_sourceFilePath = std::u8string(path.begin(), path.end()); } diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index e81f3f0c6..87a54d0e5 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -643,8 +643,8 @@ namespace hex::plugin::builtin { auto json = nlohmann::json::parse(response.getData()); links.push_back({ wolv::util::toUTF8String(currFile.filename()), - json["data"]["file"]["url"]["short"], - json["data"]["file"]["metadata"]["size"]["readable"] + json.at("data").at("file").at("url").at("short"), + json.at("data").at("file").at("metadata").at("size").at("readable") }); } catch (...) { PopupError::open("hex.builtin.tools.file_uploader.invalid_response"_lang); @@ -705,8 +705,8 @@ namespace hex::plugin::builtin { auto json = nlohmann::json::parse(response.getData()); - resultTitle = json["query"]["pages"][0]["title"].get(); - resultExtract = json["query"]["pages"][0]["extract"].get(); + resultTitle = json.at("query").at("pages").at(0).at("title").get(); + resultExtract = json.at("query").at("pages").at(0).at("extract").get(); if (!extendedSearch && resultExtract.ends_with(':')) { extendedSearch = true; diff --git a/plugins/builtin/source/content/views/view_constants.cpp b/plugins/builtin/source/content/views/view_constants.cpp index c2587f599..f3b978a3a 100644 --- a/plugins/builtin/source/content/views/view_constants.cpp +++ b/plugins/builtin/source/content/views/view_constants.cpp @@ -34,15 +34,15 @@ namespace hex::plugin::builtin { auto fileData = wolv::io::File(file.path(), wolv::io::File::Mode::Read).readString(); auto content = nlohmann::json::parse(fileData); - for (auto value : content["values"]) { + for (auto value : content.at("values")) { Constant constant; - constant.category = content["name"].get(); - constant.name = value["name"].get(); + constant.category = content.at("name").get(); + constant.name = value.at("name").get(); if (value.contains("desc")) - constant.description = value["desc"].get(); - constant.value = value["value"].get(); + constant.description = value.at("desc").get(); + constant.value = value.at("value").get(); - auto type = value["type"]; + auto type = value.at("type"); if (type == "int10") constant.type = ConstantType::Int10; else if (type == "int16be") diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 6b388dd55..ec6672adb 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -64,8 +64,8 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_name = j["name"].get(); - this->m_type = j["type"]; + this->m_name = j.at("name").get(); + this->m_type = j.at("type"); this->setUnlocalizedTitle(this->m_name); this->setAttributes({ @@ -128,8 +128,8 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_name = j["name"].get(); - this->m_type = j["type"]; + this->m_name = j.at("name").get(); + this->m_type = j.at("type"); this->setUnlocalizedTitle(this->m_name); this->setAttributes({ @@ -265,7 +265,7 @@ namespace hex::plugin::builtin { } void load(const nlohmann::json &j) override { - this->m_dataProcessor->loadNodes(this->m_workspace, j["nodes"]); + this->m_dataProcessor->loadNodes(this->m_workspace, j.at("nodes")); this->m_name = LangEntry(this->getUnlocalizedTitle()).get(); this->m_requiresAttributeUpdate = true; @@ -494,7 +494,7 @@ namespace hex::plugin::builtin { try { nlohmann::json nodeJson = nlohmann::json::parse(wolv::io::File(entry.path(), wolv::io::File::Mode::Read).readString()); - this->m_customNodes.push_back(CustomNode { LangEntry(nodeJson["name"]), nodeJson }); + this->m_customNodes.push_back(CustomNode { LangEntry(nodeJson.at("name")), nodeJson }); } catch (nlohmann::json::exception &e) { continue; } @@ -906,7 +906,7 @@ namespace hex::plugin::builtin { std::unique_ptr newNode; for (auto &entry : nodeEntries) { - if (entry.name == node["type"].get()) + if (node.contains("name") && entry.name == node["type"].get()) newNode = entry.creatorFunction(); } diff --git a/plugins/builtin/source/content/views/view_hashes.cpp b/plugins/builtin/source/content/views/view_hashes.cpp index fb70aaf42..2d2391e98 100644 --- a/plugins/builtin/source/content/views/view_hashes.cpp +++ b/plugins/builtin/source/content/views/view_hashes.cpp @@ -211,7 +211,7 @@ namespace hex::plugin::builtin { const auto &hashes = ContentRegistry::Hashes::impl::getHashes(); for (const auto &hash : json["hashes"]) { - if (!hash.contains("name") || !hash.contains("type")) + if (!hash.contains("name") || !hash.contains("type") || !hash.contains("settings")) continue; for (const auto &newHash : hashes) { diff --git a/plugins/builtin/source/content/views/view_patches.cpp b/plugins/builtin/source/content/views/view_patches.cpp index a9d62fc84..32b6f89f2 100644 --- a/plugins/builtin/source/content/views/view_patches.cpp +++ b/plugins/builtin/source/content/views/view_patches.cpp @@ -18,7 +18,7 @@ namespace hex::plugin::builtin { .required = false, .load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) { auto json = nlohmann::json::parse(tar.readString(basePath)); - provider->getPatches() = json["patches"].get>(); + provider->getPatches() = json.at("patches").get>(); return true; }, .store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) { diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index 06326bbd1..ab6631bd8 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -149,8 +149,8 @@ namespace hex::plugin::builtin { try { auto jsonData = nlohmann::json::parse(wolv::io::File(path, wolv::io::File::Mode::Read).readString()); uniqueProviders.insert(RecentProvider { - .displayName = jsonData["displayName"], - .type = jsonData["type"], + .displayName = jsonData.at("displayName"), + .type = jsonData.at("type"), .filePath = path, .data = jsonData }); @@ -592,7 +592,7 @@ namespace hex::plugin::builtin { auto days_since_epoch = std::chrono::duration_cast(now.time_since_epoch()); std::mt19937 random(days_since_epoch.count()); - auto chosenCategory = tipsCategories[random()%tipsCategories.size()]["tips"]; + auto chosenCategory = tipsCategories[random()%tipsCategories.size()].at("tips"); auto chosenTip = chosenCategory[random()%chosenCategory.size()]; s_tipOfTheDay = chosenTip.get();