db/Configured: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-10-28 23:00:10 +02:00
parent 318d0b3976
commit 78bf4ef5fa
5 changed files with 24 additions and 35 deletions

View File

@ -183,14 +183,9 @@ glue_db_init_and_load(void)
{ {
Error error; Error error;
instance->database = instance->database =
CreateConfiguredDatabase(instance->event_loop, *instance, CreateConfiguredDatabase(instance->event_loop, *instance);
error); if (instance->database == nullptr)
if (instance->database == nullptr) { return true;
if (error.IsDefined())
FatalError(error);
else
return true;
}
if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) { if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) {
InitStorage(); InitStorage();

View File

@ -23,31 +23,26 @@
#include "config/ConfigGlobal.hxx" #include "config/ConfigGlobal.hxx"
#include "config/Param.hxx" #include "config/Param.hxx"
#include "config/Block.hxx" #include "config/Block.hxx"
#include "config/ConfigError.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "fs/StandardDirectory.hxx" #include "fs/StandardDirectory.hxx"
#include "util/Error.hxx" #include "util/RuntimeError.hxx"
Database * Database *
CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener, CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener)
Error &error)
{ {
const auto *param = config_get_block(ConfigBlockOption::DATABASE); const auto *param = config_get_block(ConfigBlockOption::DATABASE);
const auto *path = config_get_param(ConfigOption::DB_FILE); const auto *path = config_get_param(ConfigOption::DB_FILE);
if (param != nullptr && path != nullptr) { if (param != nullptr && path != nullptr)
error.Format(config_domain, throw FormatRuntimeError("Found both 'database' (line %d) and 'db_file' (line %d) setting",
"Found both 'database' (line %d) and 'db_file' (line %d) setting", param->line, path->line);
param->line, path->line);
return nullptr;
}
if (param != nullptr) if (param != nullptr)
return DatabaseGlobalInit(loop, listener, *param, error); return DatabaseGlobalInit(loop, listener, *param);
else if (path != nullptr) { else if (path != nullptr) {
ConfigBlock block(path->line); ConfigBlock block(path->line);
block.AddBlockParam("path", path->value.c_str(), path->line); block.AddBlockParam("path", path->value.c_str(), path->line);
return DatabaseGlobalInit(loop, listener, block, error); return DatabaseGlobalInit(loop, listener, block);
} else { } else {
/* if there is no override, use the cache directory */ /* if there is no override, use the cache directory */
@ -63,6 +58,6 @@ CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener,
ConfigBlock block; ConfigBlock block;
block.AddBlockParam("path", db_file_utf8.c_str(), -1); block.AddBlockParam("path", db_file_utf8.c_str(), -1);
return DatabaseGlobalInit(loop, listener, block, error); return DatabaseGlobalInit(loop, listener, block);
} }
} }

View File

@ -25,15 +25,15 @@
class EventLoop; class EventLoop;
class DatabaseListener; class DatabaseListener;
class Database; class Database;
class Error;
/** /**
* Read database configuration settings and create a #Database * Read database configuration settings and create a #Database
* instance from it, but do not open it. Returns nullptr on error or * instance from it, but do not open it. Returns nullptr if no
* if no database is configured (no #Error set in that case). * database is configured.
*
* Throws #std::runtime_error on error.
*/ */
Database * Database *
CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener, CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener);
Error &error);
#endif #endif

View File

@ -21,23 +21,21 @@
#include "DatabaseGlue.hxx" #include "DatabaseGlue.hxx"
#include "Registry.hxx" #include "Registry.hxx"
#include "DatabaseError.hxx" #include "DatabaseError.hxx"
#include "util/Error.hxx" #include "util/RuntimeError.hxx"
#include "config/Block.hxx" #include "config/Block.hxx"
#include "DatabasePlugin.hxx" #include "DatabasePlugin.hxx"
Database * Database *
DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block, Error &error) const ConfigBlock &block)
{ {
const char *plugin_name = const char *plugin_name =
block.GetBlockValue("plugin", "simple"); block.GetBlockValue("plugin", "simple");
const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name); const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
if (plugin == nullptr) { if (plugin == nullptr)
error.Format(db_domain, throw FormatRuntimeError("No such database plugin: %s",
"No such database plugin: %s", plugin_name); plugin_name);
return nullptr;
}
return plugin->create(loop, listener, block); return plugin->create(loop, listener, block);
} }

View File

@ -26,15 +26,16 @@ struct ConfigBlock;
class EventLoop; class EventLoop;
class DatabaseListener; class DatabaseListener;
class Database; class Database;
class Error;
/** /**
* Initialize the database library. * Initialize the database library.
* *
* Throws #std::runtime_error on error.
*
* @param block the database configuration block * @param block the database configuration block
*/ */
Database * Database *
DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block, Error &error); const ConfigBlock &block);
#endif #endif