config/ConfigFile: use std::exception on syntax error

This commit is contained in:
Max Kellermann
2015-12-16 11:12:30 +01:00
parent 6717325c3f
commit d256a0e98f
10 changed files with 127 additions and 87 deletions

View File

@@ -27,6 +27,7 @@
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "fs/Path.hxx"
#include "fs/io/FileReader.hxx"
#include "fs/io/BufferedReader.hxx"
@@ -54,27 +55,20 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line,
const char *value = tokenizer.NextString(error);
if (value == nullptr) {
if (tokenizer.IsEnd()) {
error.Set(config_file_domain, "Value missing");
} else {
assert(error.IsDefined());
}
if (tokenizer.IsEnd())
throw std::runtime_error("Value missing");
assert(error.IsDefined());
return false;
}
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) {
error.Set(config_file_domain, "Unknown tokens after value");
return false;
}
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT)
throw std::runtime_error("Unknown tokens after value");
const BlockParam *bp = block.GetBlockParam(name);
if (bp != nullptr) {
error.Format(config_file_domain,
"\"%s\" is duplicate, first defined on line %i",
name, bp->line);
return false;
}
if (bp != nullptr)
throw FormatRuntimeError("\"%s\" is duplicate, first defined on line %i",
name, bp->line);
block.AddBlockParam(name, value, line);
return true;
@@ -82,15 +76,14 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line,
static ConfigBlock *
config_read_block(BufferedReader &reader, Error &error)
{
try {
std::unique_ptr<ConfigBlock> block(new ConfigBlock(reader.GetLineNumber()));
while (true) {
char *line = reader.ReadLine();
if (line == nullptr) {
if (reader.Check(error))
error.Set(config_file_domain,
"Expected '}' before end-of-file");
throw std::runtime_error("Expected '}' before end-of-file");
return nullptr;
}
@@ -103,12 +96,8 @@ config_read_block(BufferedReader &reader, Error &error)
(and from this "while" loop) */
line = StripLeft(line + 1);
if (*line != 0 && *line != CONF_COMMENT) {
error.Format(config_file_domain,
"line %u: Unknown tokens after '}'",
reader.GetLineNumber());
return nullptr;
}
if (*line != 0 && *line != CONF_COMMENT)
throw std::runtime_error("Unknown tokens after '}'");
return block.release();
}
@@ -123,6 +112,8 @@ config_read_block(BufferedReader &reader, Error &error)
return nullptr;
}
}
} catch (...) {
std::throw_with_nested(FormatRuntimeError("Error in line %u", reader.GetLineNumber()));
}
gcc_nonnull_all
@@ -150,30 +141,22 @@ ReadConfigBlock(ConfigData &config_data, BufferedReader &reader,
if (head != nullptr && !option.repeatable) {
ConfigBlock *block = head;
error.Format(config_file_domain,
"config parameter \"%s\" is first defined "
"on line %d and redefined on line %u\n",
name, block->line,
reader.GetLineNumber());
return false;
throw FormatRuntimeError("config parameter \"%s\" is first defined "
"on line %d and redefined on line %u\n",
name, block->line,
reader.GetLineNumber());
}
/* now parse the block or the value */
if (tokenizer.CurrentChar() != '{') {
error.Format(config_file_domain,
"line %u: '{' expected",
reader.GetLineNumber());
return false;
}
if (tokenizer.CurrentChar() != '{')
throw FormatRuntimeError("line %u: '{' expected",
reader.GetLineNumber());
char *line = StripLeft(tokenizer.Rest() + 1);
if (*line != 0 && *line != CONF_COMMENT) {
error.Format(config_file_domain,
"line %u: Unknown tokens after '{'",
reader.GetLineNumber());
return false;
}
if (*line != 0 && *line != CONF_COMMENT)
throw FormatRuntimeError("line %u: Unknown tokens after '{'",
reader.GetLineNumber());
auto *param = config_read_block(reader, error);
if (param == nullptr)
@@ -208,12 +191,10 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
if (head != nullptr && !option.repeatable) {
struct config_param *param = head;
error.Format(config_file_domain,
"config parameter \"%s\" is first defined "
"on line %d and redefined on line %u\n",
name, param->line,
reader.GetLineNumber());
return false;
throw FormatRuntimeError("config parameter \"%s\" is first defined "
"on line %d and redefined on line %u\n",
name, param->line,
reader.GetLineNumber());
}
/* now parse the block or the value */
@@ -221,23 +202,16 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
const char *value = tokenizer.NextString(error);
if (value == nullptr) {
if (tokenizer.IsEnd())
error.Format(config_file_domain,
"line %u: Value missing",
reader.GetLineNumber());
else
error.FormatPrefix("line %u: ",
reader.GetLineNumber());
throw FormatRuntimeError("line %u: Value missing",
reader.GetLineNumber());
error.FormatPrefix("line %u: ", reader.GetLineNumber());
return false;
}
if (!tokenizer.IsEnd() &&
tokenizer.CurrentChar() != CONF_COMMENT) {
error.Format(config_file_domain,
"line %u: Unknown tokens after value",
reader.GetLineNumber());
return false;
}
if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT)
throw FormatRuntimeError("line %u: Unknown tokens after value",
reader.GetLineNumber());
auto *param = new config_param(value, reader.GetLineNumber());
Append(head, param);
@@ -281,11 +255,9 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
tokenizer, error))
return false;
} else {
error.Format(config_file_domain,
"unrecognized parameter in config file at "
"line %u: %s\n",
reader.GetLineNumber(), name);
return false;
throw FormatRuntimeError("unrecognized parameter in config file at "
"line %u: %s\n",
reader.GetLineNumber(), name);
}
}
}