From 3783c687a2d3e70fa4fe2ef8706df9351abafe7a Mon Sep 17 00:00:00 2001 From: ravinsp <33562092+ravinsp@users.noreply.github.com> Date: Mon, 15 Feb 2021 15:10:44 +0530 Subject: [PATCH] Improved missing config field error reporting. --- src/conf.cpp | 86 +++++++++++++++++++++++++--------------------------- src/conf.hpp | 2 +- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index 50dcae51..6f3fee60 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -287,8 +287,12 @@ namespace conf return -1; } + std::string jpath; + // node { + jpath = "node"; + try { const jsoncons::ojson &node = d["node"]; @@ -323,7 +327,7 @@ namespace conf } catch (const std::exception &e) { - std::cerr << "Required node config field " << extract_missing_field(e.what()) << " missing at " << ctx.config_file << std::endl; + print_missing_field_error(jpath, e); return -1; } } @@ -336,6 +340,8 @@ namespace conf // mesh { + jpath = "mesh"; + try { const jsoncons::ojson &mesh = d["mesh"]; @@ -380,28 +386,22 @@ namespace conf cfg.mesh.max_bad_msgsigs_per_min = mesh["max_bad_msgsigs_per_min"].as(); cfg.mesh.max_dup_msgs_per_min = mesh["max_dup_msgs_per_min"].as(); cfg.mesh.idle_timeout = mesh["idle_timeout"].as(); - if (!mesh["peer_discovery"].contains("interval")) - { - std::cerr << "Required mesh peer discovery config field interval missing at " << ctx.config_file << std::endl; - return -1; - } + + jpath = "mesh.peer_discovery"; cfg.mesh.peer_discovery.interval = mesh["peer_discovery"]["interval"].as(); - if (!mesh["peer_discovery"].contains("enabled")) - { - std::cerr << "Required mesh peer discovery config field enabled missing at " << ctx.config_file << std::endl; - return -1; - } cfg.mesh.peer_discovery.enabled = mesh["peer_discovery"]["enabled"].as(); } catch (const std::exception &e) { - std::cerr << "Required mesh config field " << extract_missing_field(e.what()) << " missing at " << ctx.config_file << std::endl; + print_missing_field_error(jpath, e); return -1; } } // user { + jpath = "user"; + try { const jsoncons::ojson &user = d["user"]; @@ -417,13 +417,15 @@ namespace conf } catch (const std::exception &e) { - std::cerr << "Required user config field " << extract_missing_field(e.what()) << " missing at " << ctx.config_file << std::endl; + print_missing_field_error(jpath, e); return -1; } } // hpfs { + jpath = "hpfs"; + try { const jsoncons::ojson &hpfs = d["hpfs"]; @@ -431,13 +433,15 @@ namespace conf } catch (const std::exception &e) { - std::cerr << "Required hpfs config field " << extract_missing_field(e.what()) << " missing at " << ctx.config_file << std::endl; + print_missing_field_error(jpath, e); return -1; } } // log { + jpath = "log"; + try { const jsoncons::ojson &log = d["log"]; @@ -451,7 +455,7 @@ namespace conf } catch (const std::exception &e) { - std::cerr << "Required log config field " << extract_missing_field(e.what()) << " missing at " << ctx.config_file << std::endl; + print_missing_field_error(jpath, e); return -1; } } @@ -575,19 +579,18 @@ namespace conf // Other required fields. - bool fields_missing = false; + bool fields_invalid = false; + fields_invalid |= cfg.contract.roundtime == 0 && std::cerr << "Invalid value for roundtime\n"; + fields_invalid |= cfg.contract.unl.empty() && std::cerr << "Invalid value for unl. Unl list cannot be empty.\n"; + fields_invalid |= cfg.contract.id.empty() && std::cerr << "Invalid value for contract id.\n"; + fields_invalid |= cfg.mesh.port == 0 && std::cerr << "Invalid value for mesh port\n"; + fields_invalid |= cfg.user.port == 0 && std::cerr << "Invalid value for user port\n"; + fields_invalid |= cfg.log.loglevel.empty() && std::cerr << "Invalid value for loglevel\n"; + fields_invalid |= cfg.log.loggers.empty() && std::cerr << "Invalid value for loggers\n"; - fields_missing |= cfg.contract.roundtime == 0 && std::cerr << "Missing cfg field: roundtime\n"; - fields_missing |= cfg.contract.unl.empty() && std::cerr << "Missing cfg field: unl. Unl list cannot be empty.\n"; - fields_missing |= cfg.contract.id.empty() && std::cerr << "Missing cfg field: contract id.\n"; - fields_missing |= cfg.mesh.port == 0 && std::cerr << "Missing cfg field: mesh port\n"; - fields_missing |= cfg.user.port == 0 && std::cerr << "Missing cfg field: user port\n"; - fields_missing |= cfg.log.loglevel.empty() && std::cerr << "Missing cfg field: loglevel\n"; - fields_missing |= cfg.log.loggers.empty() && std::cerr << "Missing cfg field: loggers\n"; - - if (fields_missing) + if (fields_invalid) { - std::cerr << "Required configuration fields missing at " << ctx.config_file << std::endl; + std::cerr << "Invalid configuration values at " << ctx.config_file << std::endl; return -1; } @@ -705,14 +708,15 @@ namespace conf } /** - * Extracts missing config field from the jsoncons exception message. - * @param err_message Jsoncons error message. - * @return Missing config field. - */ - const std::string extract_missing_field(std::string err_message) + * Prints the config json parsing field missing error. + */ + void print_missing_field_error(std::string_view jpath, const std::exception &e) { - err_message.erase(0, err_message.find("'") + 1); - return err_message.substr(0, err_message.find("'")); + // Extract field name from jsoncons exception message. + std::string msg = e.what(); + msg.erase(0, msg.find("'") + 1); + const std::string field = msg.substr(0, msg.find("'")); + std::cerr << "Required config field '" << jpath << "." << field << "' missing at " << ctx.config_file << std::endl; } /** @@ -867,6 +871,8 @@ namespace conf */ int parse_contract_section_json(contract_config &contract, const jsoncons::ojson &jdoc, const bool is_patch_config) { + std::string jpath = "contract"; + try { if (!is_patch_config) @@ -931,26 +937,18 @@ namespace conf } contract.is_npl_public = jdoc["npl"] == PUBLIC; - if (!jdoc["appbill"].contains("mode")) - { - std::cerr << "Required contract appbill config field mode missing at " << ctx.config_file << std::endl; - return -1; - } + jpath = "contract.appbill"; contract.appbill.mode = jdoc["appbill"]["mode"].as(); - if (!jdoc["appbill"].contains("bin_args")) - { - std::cerr << "Required contract appbill config field bin_args missing at " << ctx.config_file << std::endl; - return -1; - } contract.appbill.bin_args = jdoc["appbill"]["bin_args"].as(); + jpath = "contract.round_limits"; contract.round_limits.user_input_bytes = jdoc["round_limits"]["user_input_bytes"].as(); contract.round_limits.user_output_bytes = jdoc["round_limits"]["user_output_bytes"].as(); contract.round_limits.npl_output_bytes = jdoc["round_limits"]["npl_output_bytes"].as(); } catch (const std::exception &e) { - std::cerr << "Required contract config field '" << extract_missing_field(e.what()) << "' missing at " << ctx.config_file << std::endl; + print_missing_field_error(jpath, e); return -1; } diff --git a/src/conf.hpp b/src/conf.hpp index 878da1ae..18a3f8c6 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -224,7 +224,7 @@ namespace conf LOG_SEVERITY get_loglevel_type(std::string_view severity); - const std::string extract_missing_field(std::string err_message); + void print_missing_field_error(std::string_view jpath, const std::exception &e); int populate_patch_config();