From 38f2bf37845635848c6a27cb1c68b3f3a73a4101 Mon Sep 17 00:00:00 2001 From: Shuotian Cheng Date: Tue, 8 Aug 2017 11:46:58 -0700 Subject: [PATCH] [swssconfig]: Log errors instead of throwing exception when file open fails (#277) --- swssconfig/swssconfig.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/swssconfig/swssconfig.cpp b/swssconfig/swssconfig.cpp index 0de68bbce827..02fdaaaa1362 100644 --- a/swssconfig/swssconfig.cpp +++ b/swssconfig/swssconfig.cpp @@ -184,27 +184,33 @@ int main(int argc, char **argv) { SWSS_LOG_NOTICE("Loading config from JSON file:%s...", i.c_str()); - ifstream fs; vector db_items; try { - fs.open(i, fstream::in); + ifstream fs(i); + if (!fs) + { + SWSS_LOG_ERROR("Failed to open file %s", i.c_str()); + cerr << "Failed to open file " << i.c_str() << endl; + return EXIT_FAILURE; + } + if (!load_json_db_data(fs, db_items)) { - SWSS_LOG_ERROR("Failed loading data from JSON file:%s", i.c_str()); + SWSS_LOG_ERROR("Failed loading data from JSON file %s", i.c_str()); return EXIT_FAILURE; } if (!write_db_data(db_items)) { - SWSS_LOG_ERROR("Failed applying data from JSON file:%s", i.c_str()); + SWSS_LOG_ERROR("Failed applying data from JSON file %s", i.c_str()); return EXIT_FAILURE; } } catch(const exception &e) { - SWSS_LOG_ERROR("Exception caught:%s", e.what()); - cout << "Exception caught:" << e.what() << endl; + SWSS_LOG_ERROR("Exception caught: %s", e.what()); + cout << "Exception caught: " << e.what() << endl; return EXIT_FAILURE; } }