diff --git a/include/core/yaml/parser_yaml.hpp b/include/core/yaml/parser_yaml.hpp index 42e1c343972a705140a9f66099c252001078d812..87fb0e983cb8dfd40cbf07e6b40563b476dbfa17 100644 --- a/include/core/yaml/parser_yaml.hpp +++ b/include/core/yaml/parser_yaml.hpp @@ -186,6 +186,7 @@ class ParserYAML { std::vector<std::array<std::string, 3>> _callbacks; YAML::Node problem; std::string generatePath(std::string); + YAML::Node loadYAML(std::string); public: ParserYAML(){ _params = std::map<std::string, std::string>(); @@ -252,6 +253,13 @@ std::string ParserYAML::generatePath(std::string path){ return _path_root + path; } } +YAML::Node ParserYAML::loadYAML(std::string file){ + try{ + return YAML::LoadFile(generatePath(file)); + }catch (YAML::BadFile& e){ + throw std::runtime_error("Couldn't load file " + generatePath(file)); + } +} std::string ParserYAML::tagsToString(std::vector<std::string> &tags){ std::string hdr = ""; for(auto it : tags){ @@ -262,20 +270,20 @@ std::string ParserYAML::tagsToString(std::vector<std::string> &tags){ void ParserYAML::walkTree(std::string file){ YAML::Node n; std::cout << "Parsing " << generatePath(file) << std::endl; - n = YAML::LoadFile(generatePath(file)); + n = loadYAML(generatePath(file)); std::vector<std::string> hdrs = std::vector<std::string>(); walkTreeR(n, hdrs, ""); } void ParserYAML::walkTree(std::string file, std::vector<std::string>& tags){ YAML::Node n; std::cout << "Parsing " << generatePath(file) << std::endl; - n = YAML::LoadFile(generatePath(file)); + n = loadYAML(generatePath(file)); walkTreeR(n, tags, ""); } void ParserYAML::walkTree(std::string file, std::vector<std::string>& tags, std::string hdr){ YAML::Node n; std::cout << "Parsing " << generatePath(file) << std::endl; - n = YAML::LoadFile(generatePath(file)); + n = loadYAML(generatePath(file)); walkTreeR(n, tags, hdr); } /** @Brief Recursively walks the YAML tree while filling a map with the values parsed from the file @@ -365,18 +373,28 @@ void ParserYAML::updateActiveName(std::string tag){ void ParserYAML::parseFirstLevel(std::string file){ YAML::Node n; std::cout << "Parsing " << generatePath(file) << std::endl; - n = YAML::LoadFile(generatePath(file)); + n = loadYAML(generatePath(file)); YAML::Node n_config = n["config"]; - assert(n_config.Type() == YAML::NodeType::Map && "trying to parse config node but found a non-Map node"); + // assert(n_config.Type() == YAML::NodeType::Map && "trying to parse config node but found a non-Map node"); + if(n_config.Type() != YAML::NodeType::Map) throw std::runtime_error("Could not find config node. Please make sure that your YAML file " + generatePath(file) + " starts with 'config:'"); + if(n_config["problem"].Type() != YAML::NodeType::Map) throw std::runtime_error("Could not find problem node. Please make sure that the 'config' node in YAML file " + generatePath(file) + " has a 'problem' entry"); this->problem = n_config["problem"]; - for(const auto& kv : n_config["sensors"]){ + try{ + for(const auto& kv : n_config["sensors"]){ ParamsInitSensor pSensor = {kv["type"].Scalar(), kv["name"].Scalar(), kv}; _paramsSens.push_back(pSensor); + } + } catch(YAML::InvalidNode& e){ + throw std::runtime_error("Error parsing sensors. Please make sure that each sensor entry has 'type' and 'name' entries."); } - for(const auto& kv : n_config["processors"]){ + try{ + for(const auto& kv : n_config["processors"]){ ParamsInitProcessor pProc = {kv["type"].Scalar(), kv["name"].Scalar(), kv["sensor_name"].Scalar(), kv}; _paramsProc.push_back(pProc); + } + } catch(YAML::InvalidNode& e){ + throw std::runtime_error("Error parsing processors. Please make sure that each processor has 'type', 'name' and 'sensor_name' entries."); } for(const auto& kv : n_config["callbacks"]){ _callbacks.push_back({{kv[0].as<std::string>(), kv[1].as<std::string>(), kv[2].as<std::string>()}}); diff --git a/test/yaml/params3.yaml b/test/yaml/params3.yaml index 222fd91b45a9846d0f6b571db36f2f8b680591ae..4983d0e23734df04eb87c20422e272febbdbfb67 100644 --- a/test/yaml/params3.yaml +++ b/test/yaml/params3.yaml @@ -1,7 +1,7 @@ config: - # problem: - # frame_structure: "POV" - # dimension: 2 + problem: + frame_structure: "POV" + dimension: 2 sensors: - type: "ODOM 2D"