From 367c996b38436d628e9dc4a7cd3fd024a6e9f25d Mon Sep 17 00:00:00 2001 From: Wang Xiangyu Date: Thu, 24 Sep 2020 18:01:32 +0800 Subject: [PATCH] catch-load-config-exception (#3857) Signed-off-by: Wang Xiangyu remove unused code Signed-off-by: Wang Xiangyu --- core/src/config/ConfigMgr.cpp | 40 +++++++++++++++++++++++------------ core/src/config/ConfigMgr.h | 2 ++ core/src/main.cpp | 9 +++++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/core/src/config/ConfigMgr.cpp b/core/src/config/ConfigMgr.cpp index ec10390b5a..e2a8320b5c 100644 --- a/core/src/config/ConfigMgr.cpp +++ b/core/src/config/ConfigMgr.cpp @@ -43,7 +43,7 @@ Flatten(const YAML::Node& node, std::unordered_map& ta break; } case YAML::NodeType::Undefined: { - throw "Unexpected"; + std::runtime_error("Undefined YAML Node is not supported in Flatten."); } default: break; @@ -123,27 +123,39 @@ ConfigMgr::Init() { void ConfigMgr::LoadFile(const std::string& path) { - /* load from milvus.yaml */ - auto yaml = YAML::LoadFile(path); + try { + /* load from milvus.yaml */ + auto yaml = YAML::LoadFile(path); - /* make it flattened */ - std::unordered_map flattened; - Flatten(yaml, flattened, ""); + /* make it flattened */ + std::unordered_map flattened; + Flatten(yaml, flattened, ""); - /* update config */ - for (auto& it : flattened) Set(it.first, it.second, false); + /* update config */ + for (auto& it : flattened) Set(it.first, it.second, false); + } catch (std::exception& ex) { + throw; + } catch (...) { + throw std::runtime_error("Unknown error occurred."); + } } void ConfigMgr::LoadMemory(const std::string& yaml_string) { - auto yaml = YAML::Load(yaml_string); + try { + auto yaml = YAML::Load(yaml_string); - /* make it flattened */ - std::unordered_map flattened; - Flatten(yaml, flattened, ""); + /* make it flattened */ + std::unordered_map flattened; + Flatten(yaml, flattened, ""); - /* update config */ - for (auto& it : flattened) Set(it.first, it.second, false); + /* update config */ + for (auto& it : flattened) Set(it.first, it.second, false); + } catch (std::exception& ex) { + throw; + } catch (...) { + throw std::runtime_error("Unknown error occurred."); + } } void diff --git a/core/src/config/ConfigMgr.h b/core/src/config/ConfigMgr.h index e39f5b7a08..de827f9760 100644 --- a/core/src/config/ConfigMgr.h +++ b/core/src/config/ConfigMgr.h @@ -90,10 +90,12 @@ class ConfigMgr : public BaseConfigMgr { void Init(); + /* throws std::exception only */ void LoadFile(const std::string& path); /* for testing */ + /* throws std::exception only */ void LoadMemory(const std::string& yaml_string); diff --git a/core/src/main.cpp b/core/src/main.cpp index 1f40b35ef8..44e71a22a7 100644 --- a/core/src/main.cpp +++ b/core/src/main.cpp @@ -93,7 +93,6 @@ main(int argc, char* argv[]) { char* config_filename_ptr = strdup(optarg); config_filename = config_filename_ptr; free(config_filename_ptr); - std::cout << "Loading configuration from: " << config_filename << std::endl; break; } case 'p': { @@ -134,8 +133,12 @@ main(int argc, char* argv[]) { milvus::ConfigMgr::GetInstance().Init(); milvus::ConfigMgr::GetInstance().LoadFile(config_filename); milvus::ConfigMgr::GetInstance().FilePath() = config_filename; - } catch (milvus::ConfigStatus& cs) { - std::cerr << "Load config(" << config_filename << ") failed: " << cs.message << std::endl; + std::cout << "Successfully load configuration from " << config_filename << "." << std::endl; + } catch (std::exception& ex) { + std::cerr << "Load configuration file " << config_filename << " failed: " << ex.what() << std::endl; + goto FAIL; + } catch (...) { + std::cerr << "Load configuration file " << config_filename << " failed: Unknown error." << std::endl; goto FAIL; }