diff options
author | deva <deva> | 2011-02-15 08:15:12 +0000 |
---|---|---|
committer | deva <deva> | 2011-02-15 08:15:12 +0000 |
commit | 3c55ea163a765c3cf68d51601bb64ebb9c201e41 (patch) | |
tree | e718db47c9515d2ff27eb6fa22ac2349d3d29c58 /server/src | |
parent | 634d7661ec1d6d202685cafb958b5a55dc2498aa (diff) |
Improved error messages from ConfigrationParser.
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/configurationparser.cc | 78 | ||||
-rw-r--r-- | server/src/configurationparser.h | 38 | ||||
-rw-r--r-- | server/src/pracrod.cc | 14 |
3 files changed, 76 insertions, 54 deletions
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 <test.h> #define CONFFILE "/tmp/configurationparser.conf" +#define NOSUCH_CONFFILE "/tmp/ladida_configurationparser.conf" #include <sys/types.h> #include <sys/stat.h> @@ -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 <libconfig.h++> #include <string> -#include "exception.h" - -/** - * This exception is thrown by Configuration when reload fails. - */ -class ConfigurationParserException: public Exception { -public: - ConfigurationParserException(std::string reason) : - Exception(reason) {} -}; +#include <exception> /** * This is the pentominos configuration class.\n @@ -51,16 +42,39 @@ 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; } |