Handling more kinds of errors during file parsing.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 14 Mar 2012 23:31:41 +0000 (20:31 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 14 Mar 2012 23:34:46 +0000 (20:34 -0300)
src/config/configurationfile.cpp

index ab7689a..b51c0b2 100644 (file)
@@ -30,6 +30,7 @@
 #include "config/configurationoptions.h"
 
 using namespace std;
+using boost::program_options::invalid_option_value;
 using boost::program_options::options_description;
 using boost::program_options::parsed_options;
 using boost::program_options::parse_config_file;
@@ -68,18 +69,21 @@ ConfigurationFile::~ConfigurationFile()
  */
 bool ConfigurationFile::process( variables_map *vm )
 {
+    BOOST_ASSERT( !FileName.empty() );
     BOOST_ASSERT( vm != NULL );
 
-    ifstream ifs( FileName.c_str() );
-    if ( !ifs )
-    {
-        GlobalLogger.error() << "Error: could not open " << FileName
-            << " file." << endl;
-        return false;
-    }
+    ifstream ifs;
 
     try
     {
+        ifs.open( FileName.c_str() );
+        if ( !ifs.good() )
+        {
+            GlobalLogger.error() << "Error: could not open " << FileName
+                    << " file." << endl;
+            return false;
+        }
+
         ConfigurationOptions options;
 
         options_description config = options.get_configuration_options();
@@ -90,11 +94,22 @@ bool ConfigurationFile::process( variables_map *vm )
         store( parsed_opt, *vm );
         notify( *vm );
     }
-    catch ( const std::exception &ex )
+    catch ( const invalid_option_value &e )
+    {
+        GlobalLogger.error() << "Error: invalid option value exception thrown parsing config file:"
+                << e.what() << endl;
+        return false;
+    }
+    catch ( const exception &ex )
     {
         GlobalLogger.error() << ex.what() << endl;
         return false;
     }
+    catch ( ... )
+    {
+        GlobalLogger.error() << "Unknown exception" << endl;
+        return false;
+    }
 
     return true;
 }