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 +++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 39 deletions(-) (limited to 'server/src/configurationparser.cc') 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*/ -- cgit v1.2.3