Commit e17805f2 authored by Max Kellermann's avatar Max Kellermann

config/Block: GetPath() throws exception on error

parent d8bcdca5
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "ConfigPath.hxx" #include "ConfigPath.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include <assert.h> #include <assert.h>
...@@ -91,11 +92,8 @@ ConfigBlock::GetBlockValue(const char *name, const char *default_value) const ...@@ -91,11 +92,8 @@ ConfigBlock::GetBlockValue(const char *name, const char *default_value) const
} }
AllocatedPath AllocatedPath
ConfigBlock::GetPath(const char *name, const char *default_value, ConfigBlock::GetPath(const char *name, const char *default_value) const
Error &error) const
{ {
assert(!error.IsDefined());
int line2 = line; int line2 = line;
const char *s; const char *s;
...@@ -110,20 +108,15 @@ ConfigBlock::GetPath(const char *name, const char *default_value, ...@@ -110,20 +108,15 @@ ConfigBlock::GetPath(const char *name, const char *default_value,
s = default_value; s = default_value;
} }
Error error;
AllocatedPath path = ParsePath(s, error); AllocatedPath path = ParsePath(s, error);
if (gcc_unlikely(path.IsNull())) if (gcc_unlikely(path.IsNull()))
error.FormatPrefix("Invalid path in \"%s\" at line %i: ", throw FormatRuntimeError("Invalid path in \"%s\" at line %i: %s",
name, line2); name, line2, error.GetMessage());
return path; return path;
} }
AllocatedPath
ConfigBlock::GetPath(const char *name, Error &error) const
{
return GetPath(name, nullptr, error);
}
int int
ConfigBlock::GetBlockValue(const char *name, int default_value) const ConfigBlock::GetBlockValue(const char *name, int default_value) const
{ {
......
...@@ -111,11 +111,11 @@ struct ConfigBlock { ...@@ -111,11 +111,11 @@ struct ConfigBlock {
/** /**
* Same as config_get_path(), but looks up the setting in the * Same as config_get_path(), but looks up the setting in the
* specified block. * specified block.
*
* Throws #std::runtime_error on error.
*/ */
AllocatedPath GetPath(const char *name, const char *default_value, AllocatedPath GetPath(const char *name,
Error &error) const; const char *default_value=nullptr) const;
AllocatedPath GetPath(const char *name, Error &error) const;
gcc_pure gcc_pure
int GetBlockValue(const char *name, int default_value) const; int GetBlockValue(const char *name, int default_value) const;
......
...@@ -78,39 +78,27 @@ inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path, ...@@ -78,39 +78,27 @@ inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path,
Database * Database *
SimpleDatabase::Create(gcc_unused EventLoop &loop, SimpleDatabase::Create(gcc_unused EventLoop &loop,
gcc_unused DatabaseListener &listener, gcc_unused DatabaseListener &listener,
const ConfigBlock &block, Error &error) const ConfigBlock &block, Error &)
{ {
SimpleDatabase *db = new SimpleDatabase(); SimpleDatabase *db = new SimpleDatabase();
if (!db->Configure(block, error)) { db->Configure(block);
delete db;
db = nullptr;
}
return db; return db;
} }
bool void
SimpleDatabase::Configure(const ConfigBlock &block, Error &error) SimpleDatabase::Configure(const ConfigBlock &block)
{ {
path = block.GetPath("path", error); path = block.GetPath("path");
if (path.IsNull()) { if (path.IsNull())
if (!error.IsDefined()) throw std::runtime_error("No \"path\" parameter specified");
error.Set(simple_db_domain,
"No \"path\" parameter specified");
return false;
}
path_utf8 = path.ToUTF8(); path_utf8 = path.ToUTF8();
cache_path = block.GetPath("cache_directory", error); cache_path = block.GetPath("cache_directory");
if (path.IsNull() && error.IsDefined())
return false;
#ifdef ENABLE_ZLIB #ifdef ENABLE_ZLIB
compress = block.GetBlockValue("compress", compress); compress = block.GetBlockValue("compress", compress);
#endif #endif
return true;
} }
void void
......
...@@ -133,7 +133,7 @@ public: ...@@ -133,7 +133,7 @@ public:
} }
private: private:
bool Configure(const ConfigBlock &block, Error &error); void Configure(const ConfigBlock &block);
void Check() const; void Check() const;
......
...@@ -85,12 +85,9 @@ static bool ...@@ -85,12 +85,9 @@ static bool
sidplay_init(const ConfigBlock &block) sidplay_init(const ConfigBlock &block)
{ {
/* read the songlengths database file */ /* read the songlengths database file */
Error error; const auto database_path = block.GetPath("songlength_database");
const auto database_path = block.GetPath("songlength_database", error);
if (!database_path.IsNull()) if (!database_path.IsNull())
songlength_database = sidplay_load_songlength_db(database_path); songlength_database = sidplay_load_songlength_db(database_path);
else if (error.IsDefined())
FatalError(error);
default_songlength = block.GetBlockValue("default_songlength", 0u); default_songlength = block.GetBlockValue("default_songlength", 0u);
......
...@@ -40,13 +40,9 @@ static constexpr unsigned WILDMIDI_SAMPLE_RATE = 48000; ...@@ -40,13 +40,9 @@ static constexpr unsigned WILDMIDI_SAMPLE_RATE = 48000;
static bool static bool
wildmidi_init(const ConfigBlock &block) wildmidi_init(const ConfigBlock &block)
{ {
Error error;
const AllocatedPath path = const AllocatedPath path =
block.GetPath("config_file", block.GetPath("config_file",
"/etc/timidity/timidity.cfg", "/etc/timidity/timidity.cfg");
error);
if (path.IsNull())
FatalError(error);
if (!FileExists(path)) { if (!FileExists(path)) {
const auto utf8 = path.ToUTF8(); const auto utf8 = path.ToUTF8();
......
...@@ -183,14 +183,10 @@ FifoOutput::Create(const ConfigBlock &block, Error &error) ...@@ -183,14 +183,10 @@ FifoOutput::Create(const ConfigBlock &block, Error &error)
{ {
FifoOutput *fd = new FifoOutput(); FifoOutput *fd = new FifoOutput();
fd->path = block.GetPath("path", error); fd->path = block.GetPath("path");
if (fd->path.IsNull()) { if (fd->path.IsNull()) {
delete fd; delete fd;
throw std::runtime_error("No \"path\" parameter specified");
if (!error.IsDefined())
error.Set(config_domain,
"No \"path\" parameter specified");
return nullptr;
} }
fd->path_utf8 = fd->path.ToUTF8(); fd->path_utf8 = fd->path.ToUTF8();
......
...@@ -128,9 +128,7 @@ RecorderOutput::Configure(const ConfigBlock &block, Error &error) ...@@ -128,9 +128,7 @@ RecorderOutput::Configure(const ConfigBlock &block, Error &error)
return false; return false;
} }
path = block.GetPath("path", error); path = block.GetPath("path");
if (error.IsDefined())
return false;
const char *fmt = block.GetBlockValue("format_path", nullptr); const char *fmt = block.GetBlockValue("format_path", nullptr);
if (fmt != nullptr) if (fmt != nullptr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment