From 9378cf06ba0d3484f737bdc007be56e49dacca01 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 14 Mar 2012 20:31:41 -0300 Subject: [PATCH] Handling more kinds of errors during file parsing. --- src/config/configurationfile.cpp | 31 +++++++++++++++++++++++-------- 1 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/config/configurationfile.cpp b/src/config/configurationfile.cpp index ab7689a..b51c0b2 100644 --- a/src/config/configurationfile.cpp +++ b/src/config/configurationfile.cpp @@ -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; } -- 1.7.1