Improve error handling and reporting

Change-Id: I41a58409c1d488ab2038ae534851e1ab59c6d952
diff --git a/src/conf-file-processor.cpp b/src/conf-file-processor.cpp
index ec1718b..4140e0d 100644
--- a/src/conf-file-processor.cpp
+++ b/src/conf-file-processor.cpp
@@ -21,11 +21,12 @@
 
 #include "conf-file-processor.hpp"
 #include "adjacent.hpp"
-#include "utility/name-helper.hpp"
 #include "update/prefix-update-processor.hpp"
+#include "utility/name-helper.hpp"
 
 #include <ndn-cxx/name.hpp>
 #include <ndn-cxx/net/face-uri.hpp>
+#include <ndn-cxx/util/io.hpp>
 
 #include <boost/filesystem.hpp>
 #include <boost/property_tree/info_parser.hpp>
@@ -144,24 +145,19 @@
 bool
 ConfFileProcessor::processConfFile()
 {
-  bool ret = true;
-  std::ifstream inputFile;
-  inputFile.open(m_confFileName.c_str());
+  std::ifstream inputFile(m_confFileName);
   if (!inputFile.is_open()) {
-    std::string msg = "Failed to read configuration file: ";
-    msg += m_confFileName;
-    std::cerr << msg << std::endl;
+    std::cerr << "Failed to read configuration file: " << m_confFileName << std::endl;
     return false;
   }
-  ret = load(inputFile);
-  inputFile.close();
 
-  if (ret) {
-    m_confParam.buildRouterAndSyncUserPrefix();
-    m_confParam.writeLog();
+  if (!load(inputFile)) {
+    return false;
   }
 
-  return ret;
+  m_confParam.buildRouterAndSyncUserPrefix();
+  m_confParam.writeLog();
+  return true;
 }
 
 bool
@@ -171,10 +167,9 @@
   try {
     boost::property_tree::read_info(input, pt);
   }
-  catch (const boost::property_tree::info_parser_error& error) {
-    std::stringstream msg;
-    std::cerr << "Failed to parse configuration file " << std::endl;
-    std::cerr << m_confFileName << std::endl;
+  catch (const boost::property_tree::ptree_error& e) {
+    std::cerr << "Failed to parse configuration file '" << m_confFileName
+              << "': " << e.what() << std::endl;
     return false;
   }
 
@@ -190,33 +185,26 @@
 ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
 {
   bool ret = true;
-  if (sectionName == "general")
-  {
+  if (sectionName == "general") {
     ret = processConfSectionGeneral(section);
   }
-  else if (sectionName == "neighbors")
-  {
+  else if (sectionName == "neighbors") {
     ret = processConfSectionNeighbors(section);
   }
-  else if (sectionName == "hyperbolic")
-  {
+  else if (sectionName == "hyperbolic") {
     ret = processConfSectionHyperbolic(section);
   }
-  else if (sectionName == "fib")
-  {
+  else if (sectionName == "fib") {
     ret = processConfSectionFib(section);
   }
-  else if (sectionName == "advertising")
-  {
+  else if (sectionName == "advertising") {
     ret = processConfSectionAdvertising(section);
   }
-  else if (sectionName == "security")
-  {
+  else if (sectionName == "security") {
     ret = processConfSectionSecurity(section);
   }
-  else
-  {
-    std::cerr << "Wrong configuration section: " << sectionName << std::endl;
+  else {
+    std::cerr << "Unknown configuration section: " << sectionName << std::endl;
   }
   return ret;
 }
@@ -233,7 +221,7 @@
       m_confParam.setNetwork(networkName);
     }
     else {
-      std::cerr << " Network can not be null or empty or in bad URI format :(!" << std::endl;
+      std::cerr << "Network can not be null or empty or in bad URI format" << std::endl;
       return false;
     }
     ndn::Name siteName(site);
@@ -241,7 +229,7 @@
       m_confParam.setSiteName(siteName);
     }
     else {
-      std::cerr << "Site can not be null or empty or in bad URI format:( !" << std::endl;
+      std::cerr << "Site can not be null or empty or in bad URI format" << std::endl;
       return false;
     }
     ndn::Name routerName(router);
@@ -249,7 +237,7 @@
       m_confParam.setRouterName(routerName);
     }
     else {
-      std::cerr << " Router name can not be null or empty or in bad URI format:( !" << std::endl;
+      std::cerr << "Router name can not be null or empty or in bad URI format" << std::endl;
       return false;
     }
   }
@@ -265,15 +253,14 @@
     m_confParam.setLsaRefreshTime(lsaRefreshTime);
   }
   else {
-    std::cerr << "Wrong value for lsa-refresh-time ";
-    std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";
-    std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
-
+    std::cerr << "Invalid value for lsa-refresh-time. "
+              << "Allowed range: " << LSA_REFRESH_TIME_MIN
+              << "-" << LSA_REFRESH_TIME_MAX << std::endl;
     return false;
   }
 
   // router-dead-interval
-  uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
+  uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", 2 * lsaRefreshTime);
 
   if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
     m_confParam.setRouterDeadInterval(routerDeadInterval);
@@ -290,10 +277,9 @@
     m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
   }
   else {
-    std::cerr << "Wrong value for lsa-interest-timeout. "
-              << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
-              << LSA_INTEREST_LIFETIME_MAX << std::endl;
-
+    std::cerr << "Invalid value for lsa-interest-timeout. "
+              << "Allowed range: " << LSA_INTEREST_LIFETIME_MIN
+              << "-" << LSA_INTEREST_LIFETIME_MAX << std::endl;
     return false;
   }
 
@@ -312,7 +298,7 @@
     m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
   }
   else {
-    std::cerr << "Sync protocol " << syncProtocol << " is not supported!"
+    std::cerr << "Sync protocol '" << syncProtocol << "' is not supported!\n"
               << "Use chronosync or psync" << std::endl;
     return false;
   }
@@ -325,10 +311,9 @@
     m_confParam.setSyncInterestLifetime(syncInterestLifetime);
   }
   else {
-    std::cerr << "Wrong value for sync-interest-lifetime. "
-              << "Allowed value:" << SYNC_INTEREST_LIFETIME_MIN << "-"
-              << SYNC_INTEREST_LIFETIME_MAX << std::endl;
-
+    std::cerr << "Invalid value for sync-interest-lifetime. "
+              << "Allowed range: " << SYNC_INTEREST_LIFETIME_MIN
+              << "-" << SYNC_INTEREST_LIFETIME_MAX << std::endl;
     return false;
   }
 
@@ -405,10 +390,8 @@
     m_confParam.setInterestRetryNumber(retrials);
   }
   else {
-    std::cerr << "Wrong value for hello-retries." << std::endl;
-    std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
-    std::cerr << HELLO_RETRIES_MAX << std::endl;
-
+    std::cerr << "Invalid value for hello-retries. "
+              << "Allowed range: " << HELLO_RETRIES_MIN << "-" << HELLO_RETRIES_MAX << std::endl;
     return false;
   }
 
@@ -419,10 +402,8 @@
     m_confParam.setInterestResendTime(timeOut);
   }
   else {
-    std::cerr << "Wrong value for hello-timeout. ";
-    std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
-    std::cerr << HELLO_TIMEOUT_MAX << std::endl;
-
+    std::cerr << "Invalid value for hello-timeout. "
+              << "Allowed range: " << HELLO_TIMEOUT_MIN << "-" << HELLO_TIMEOUT_MAX << std::endl;
     return false;
   }
 
@@ -433,10 +414,8 @@
     m_confParam.setInfoInterestInterval(interval);
   }
   else {
-    std::cerr << "Wrong value for hello-interval. ";
-    std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
-    std::cerr << HELLO_INTERVAL_MAX << std::endl;
-
+    std::cerr << "Invalid value for hello-interval. "
+              << "Allowed range: " << HELLO_INTERVAL_MIN << "-" << HELLO_INTERVAL_MAX << std::endl;
     return false;
   }
 
@@ -454,8 +433,7 @@
   // Set the retry count for fetching the FaceStatus dataset
   ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
                                                         std::bind(&ConfParameter::setFaceDatasetFetchTries,
-                                                                  &m_confParam,
-                                                                  _1));
+                                                                  &m_confParam, _1));
 
   faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
                                           FACE_DATASET_FETCH_TRIES_MAX);
@@ -468,8 +446,7 @@
   // Set the interval between FaceStatus dataset fetch attempts.
   ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
                                                            std::bind(&ConfParameter::setFaceDatasetFetchInterval,
-                                                                     &m_confParam,
-                                                                     _1));
+                                                                     &m_confParam, _1));
 
   faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
                                              FACE_DATASET_FETCH_INTERVAL_MAX);
@@ -542,13 +519,12 @@
   else if (boost::iequals(state, "on")) {
     m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
   }
-  else if (state == "dry-run") {
+  else if (boost::iequals(state, "dry-run")) {
     m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
   }
   else {
-    std::cerr << "Wrong format for hyperbolic state." << std::endl;
-    std::cerr << "Allowed value: off, on, dry-run" << std::endl;
-
+    std::cerr << "Invalid setting for hyperbolic state. "
+              << "Allowed values: off, on, dry-run" << std::endl;
     return false;
   }
 
@@ -594,14 +570,13 @@
   int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
 
   if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
-      maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
-  {
+      maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX) {
     m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
   }
   else {
-    std::cerr << "Wrong value for max-faces-per-prefix. ";
-    std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
-
+    std::cerr << "Invalid value for max-faces-per-prefix. "
+              << "Allowed range: " << MAX_FACES_PER_PREFIX_MIN
+              << "-" << MAX_FACES_PER_PREFIX_MAX << std::endl;
     return false;
   }
 
@@ -646,10 +621,10 @@
 bool
 ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
 {
-  ConfigSection::const_iterator it = section.begin();
+  auto it = section.begin();
 
   if (it == section.end() || it->first != "validator") {
-    std::cerr << "Error: Expect validator section!" << std::endl;
+    std::cerr << "Error: Expected validator section!" << std::endl;
     return false;
   }
 
@@ -661,22 +636,26 @@
 
     it++;
     for (; it != section.end(); it++) {
-
       if (it->first != "cert-to-publish") {
-        std::cerr << "Error: Expect cert-to-publish!" << std::endl;
+        std::cerr << "Error: Expected cert-to-publish!" << std::endl;
         return false;
       }
 
       std::string file = it->second.data();
       bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
-      auto idCert = ndn::io::load<ndn::security::Certificate>(certfilePath.string());
+      std::ifstream ifs(certfilePath.string());
 
-      if (idCert == nullptr) {
-        std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
+      ndn::security::Certificate idCert;
+      try {
+        idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
+      }
+      catch (const std::exception& e) {
+        std::cerr << "Error: Cannot load cert-to-publish '" << file << "': " << e.what() << std::endl;
         return false;
       }
+
       m_confParam.addCertPath(certfilePath.string());
-      m_confParam.loadCertToValidator(*idCert);
+      m_confParam.loadCertToValidator(idCert);
     }
   }