config/Block: GetPath() throws exception on error

This commit is contained in:
Max Kellermann 2016-10-28 23:08:42 +02:00
parent d8bcdca55a
commit e17805f208
8 changed files with 23 additions and 55 deletions

View File

@ -23,6 +23,7 @@
#include "ConfigPath.hxx"
#include "system/FatalError.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include <assert.h>
@ -91,11 +92,8 @@ ConfigBlock::GetBlockValue(const char *name, const char *default_value) const
}
AllocatedPath
ConfigBlock::GetPath(const char *name, const char *default_value,
Error &error) const
ConfigBlock::GetPath(const char *name, const char *default_value) const
{
assert(!error.IsDefined());
int line2 = line;
const char *s;
@ -110,20 +108,15 @@ ConfigBlock::GetPath(const char *name, const char *default_value,
s = default_value;
}
Error error;
AllocatedPath path = ParsePath(s, error);
if (gcc_unlikely(path.IsNull()))
error.FormatPrefix("Invalid path in \"%s\" at line %i: ",
name, line2);
throw FormatRuntimeError("Invalid path in \"%s\" at line %i: %s",
name, line2, error.GetMessage());
return path;
}
AllocatedPath
ConfigBlock::GetPath(const char *name, Error &error) const
{
return GetPath(name, nullptr, error);
}
int
ConfigBlock::GetBlockValue(const char *name, int default_value) const
{

View File

@ -111,11 +111,11 @@ struct ConfigBlock {
/**
* Same as config_get_path(), but looks up the setting in the
* specified block.
*
* Throws #std::runtime_error on error.
*/
AllocatedPath GetPath(const char *name, const char *default_value,
Error &error) const;
AllocatedPath GetPath(const char *name, Error &error) const;
AllocatedPath GetPath(const char *name,
const char *default_value=nullptr) const;
gcc_pure
int GetBlockValue(const char *name, int default_value) const;

View File

@ -78,39 +78,27 @@ inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path,
Database *
SimpleDatabase::Create(gcc_unused EventLoop &loop,
gcc_unused DatabaseListener &listener,
const ConfigBlock &block, Error &error)
const ConfigBlock &block, Error &)
{
SimpleDatabase *db = new SimpleDatabase();
if (!db->Configure(block, error)) {
delete db;
db = nullptr;
}
db->Configure(block);
return db;
}
bool
SimpleDatabase::Configure(const ConfigBlock &block, Error &error)
void
SimpleDatabase::Configure(const ConfigBlock &block)
{
path = block.GetPath("path", error);
if (path.IsNull()) {
if (!error.IsDefined())
error.Set(simple_db_domain,
"No \"path\" parameter specified");
return false;
}
path = block.GetPath("path");
if (path.IsNull())
throw std::runtime_error("No \"path\" parameter specified");
path_utf8 = path.ToUTF8();
cache_path = block.GetPath("cache_directory", error);
if (path.IsNull() && error.IsDefined())
return false;
cache_path = block.GetPath("cache_directory");
#ifdef ENABLE_ZLIB
compress = block.GetBlockValue("compress", compress);
#endif
return true;
}
void

View File

@ -133,7 +133,7 @@ public:
}
private:
bool Configure(const ConfigBlock &block, Error &error);
void Configure(const ConfigBlock &block);
void Check() const;

View File

@ -85,12 +85,9 @@ static bool
sidplay_init(const ConfigBlock &block)
{
/* read the songlengths database file */
Error error;
const auto database_path = block.GetPath("songlength_database", error);
const auto database_path = block.GetPath("songlength_database");
if (!database_path.IsNull())
songlength_database = sidplay_load_songlength_db(database_path);
else if (error.IsDefined())
FatalError(error);
default_songlength = block.GetBlockValue("default_songlength", 0u);

View File

@ -40,13 +40,9 @@ static constexpr unsigned WILDMIDI_SAMPLE_RATE = 48000;
static bool
wildmidi_init(const ConfigBlock &block)
{
Error error;
const AllocatedPath path =
block.GetPath("config_file",
"/etc/timidity/timidity.cfg",
error);
if (path.IsNull())
FatalError(error);
"/etc/timidity/timidity.cfg");
if (!FileExists(path)) {
const auto utf8 = path.ToUTF8();

View File

@ -183,14 +183,10 @@ FifoOutput::Create(const ConfigBlock &block, Error &error)
{
FifoOutput *fd = new FifoOutput();
fd->path = block.GetPath("path", error);
fd->path = block.GetPath("path");
if (fd->path.IsNull()) {
delete fd;
if (!error.IsDefined())
error.Set(config_domain,
"No \"path\" parameter specified");
return nullptr;
throw std::runtime_error("No \"path\" parameter specified");
}
fd->path_utf8 = fd->path.ToUTF8();

View File

@ -128,9 +128,7 @@ RecorderOutput::Configure(const ConfigBlock &block, Error &error)
return false;
}
path = block.GetPath("path", error);
if (error.IsDefined())
return false;
path = block.GetPath("path");
const char *fmt = block.GetBlockValue("format_path", nullptr);
if (fmt != nullptr)