From 3c55ea163a765c3cf68d51601bb64ebb9c201e41 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 15 Feb 2011 08:15:12 +0000 Subject: Improved error messages from ConfigrationParser. --- server/src/configurationparser.cc | 78 +++++++++++++++++++-------------------- server/src/configurationparser.h | 38 +++++++++++++------ server/src/pracrod.cc | 14 +++++-- 3 files changed, 76 insertions(+), 54 deletions(-) (limited to 'server/src') diff --git a/server/src/configurationparser.cc b/server/src/configurationparser.cc index bc10e2b..0796cb9 100644 --- a/server/src/configurationparser.cc +++ b/server/src/configurationparser.cc @@ -28,7 +28,18 @@ #include "configuration.h" +const char *ConfigurationParser::ParseException::what() + throw() +{ + char lineno[32]; + sprintf(lineno, "%d", l); + _what = "Error when parsing the config file at line "; + _what += std::string(lineno) + ": " + e; + return _what.c_str(); +} + ConfigurationParser::ConfigurationParser(std::string filename) + throw(ParseException, ReadException) { this->filename = filename; @@ -36,17 +47,14 @@ ConfigurationParser::ConfigurationParser(std::string filename) } void ConfigurationParser::reload() - throw(ConfigurationParserException) + throw(ParseException, ReadException) { try { readFile(this->filename.c_str()); } catch(libconfig::FileIOException) { - throw ConfigurationParserException("Could not read config file: File does not exist."); + throw ReadException(); } catch(libconfig::ParseException &e) { - char lineno[32]; - sprintf(lineno, "%d", e.getLine()); - throw ConfigurationParserException(std::string("Error when parsing the config file in line ") - + lineno + ": " + e.getError()); + throw ParseException(e.getLine(), e.getError()); } // Set internal values @@ -190,8 +198,13 @@ void ConfigurationParser::reload() } #ifdef TEST_CONFIGURATIONPARSER +//deps: configuration.cc +//cflags: -I.. $(CONFIG_CXXFLAGS) +//libs: $(CONFIG_LIBS) +#include #define CONFFILE "/tmp/configurationparser.conf" +#define NOSUCH_CONFFILE "/tmp/ladida_configurationparser.conf" #include #include @@ -212,41 +225,28 @@ static char confbad[] = "c = true;\n" ; -int main() -{ - FILE *fp = fopen(CONFFILE, "w"); - if(!fp) { - printf("Could not write to %s\n", CONFFILE); - return 1; - } - fprintf(fp, conf); - fclose(fp); - try { - ConfigurationParser parser(CONFFILE); - } catch(Exception &e) { - printf("%s\n", e.what()); - return 1; - } +TEST_BEGIN; - fp = fopen(CONFFILE, "w"); - if(!fp) { - printf("Could not write to %s\n", CONFFILE); - return 1; - } - fprintf(fp, confbad); - fclose(fp); - try { - ConfigurationParser parser(CONFFILE); - } catch(Exception &e) { - printf("%s\n", e.what()); - goto on; - } - return 1; - on: +FILE *fp = fopen(CONFFILE, "w"); +if(!fp) TEST_FATAL("Could not write to "CONFFILE"\n"); +fprintf(fp, "%s", conf); +fclose(fp); - unlink(CONFFILE); +TEST_NOEXCEPTION(ConfigurationParser parser(CONFFILE), "Creation"); - return 0; -} +fp = fopen(CONFFILE, "w"); +if(!fp) TEST_FATAL("Could not write to "CONFFILE"\n"); +fprintf(fp, "%s", confbad); +fclose(fp); + +TEST_EXCEPTION(ConfigurationParser parser(CONFFILE), + ConfigurationParser::ParseException, "Bad syntax"); + +TEST_EXCEPTION(ConfigurationParser parser(NOSUCH_CONFFILE), + ConfigurationParser::ReadException, "No such file"); + +unlink(CONFFILE); + +TEST_END; #endif/*TEST_CONFIGURATIONPARSER*/ diff --git a/server/src/configurationparser.h b/server/src/configurationparser.h index 924c041..8e0c01d 100644 --- a/server/src/configurationparser.h +++ b/server/src/configurationparser.h @@ -30,16 +30,7 @@ #include #include -#include "exception.h" - -/** - * This exception is thrown by Configuration when reload fails. - */ -class ConfigurationParserException: public Exception { -public: - ConfigurationParserException(std::string reason) : - Exception(reason) {} -}; +#include /** * This is the pentominos configuration class.\n @@ -50,17 +41,40 @@ public: */ class ConfigurationParser : public libconfig::Config { public: + /** + * This exception is thrown by Configuration when reload fails. + */ + class ParseException: public std::exception { + public: + ParseException(int line, std::string err) throw() : l(line), e(err) {} + ~ParseException() throw() {} + const char *what() throw(); + + private: + std::string _what; + int l; + std::string e; + }; + + /** + * This exception is thrown by Configuration when file read fails. + */ + class ReadException: public std::exception {}; + + /** * Constructor.\n * @param filename The filename to be loaded. */ - ConfigurationParser(std::string filename); + ConfigurationParser(std::string filename) + throw(ParseException, ReadException); /** * reload, simply reloads the configuration file attached to the configuration * object. */ - void reload() throw(ConfigurationParserException); + void reload() + throw(ParseException, ReadException); private: std::string filename; diff --git a/server/src/pracrod.cc b/server/src/pracrod.cc index 73fe259..cfdafdd 100644 --- a/server/src/pracrod.cc +++ b/server/src/pracrod.cc @@ -296,8 +296,16 @@ int main(int argc, char *argv[]) } // Load config - if(configfile) configparser = new ConfigurationParser(configfile); - else configparser = new ConfigurationParser(ETC"/pracrod.conf"); + try { + if(configfile) configparser = new ConfigurationParser(configfile); + else configparser = new ConfigurationParser(ETC"/pracrod.conf"); + } catch(ConfigurationParser::ParseException &e) { + ERR_LOG(pracrod, "Config file parse error: %s.\n", e.what()); + return 1; + } catch(ConfigurationParser::ReadException &e) { + ERR_LOG(pracrod, "Config file read error: %s.\n", e.what()); + return 1; + } if(sessionpath != "") { Conf::session_path = sessionpath; @@ -308,7 +316,7 @@ int main(int argc, char *argv[]) } if(Conf::database_backend == "testdb") { - // Test db (memory only db) does not work in plural. + WARN(pracrod, "Test db (memory only db) does not work in plural.\n"); Conf::database_poolsize = 1; } -- cgit v1.2.3