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;
instance->database =
CreateConfiguredDatabase(instance->event_loop, *instance,
error);
if (instance->database == nullptr) {
if (error.IsDefined())
FatalError(error);
else
return true;
}
CreateConfiguredDatabase(instance->event_loop, *instance);
if (instance->database == nullptr)
return true;
if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) {
InitStorage();

View File

@ -23,31 +23,26 @@
#include "config/ConfigGlobal.hxx"
#include "config/Param.hxx"
#include "config/Block.hxx"
#include "config/ConfigError.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/StandardDirectory.hxx"
#include "util/Error.hxx"
#include "util/RuntimeError.hxx"
Database *
CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener,
Error &error)
CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener)
{
const auto *param = config_get_block(ConfigBlockOption::DATABASE);
const auto *path = config_get_param(ConfigOption::DB_FILE);
if (param != nullptr && path != nullptr) {
error.Format(config_domain,
"Found both 'database' (line %d) and 'db_file' (line %d) setting",
param->line, path->line);
return nullptr;
}
if (param != nullptr && path != nullptr)
throw FormatRuntimeError("Found both 'database' (line %d) and 'db_file' (line %d) setting",
param->line, path->line);
if (param != nullptr)
return DatabaseGlobalInit(loop, listener, *param, error);
return DatabaseGlobalInit(loop, listener, *param);
else if (path != nullptr) {
ConfigBlock block(path->line);
block.AddBlockParam("path", path->value.c_str(), path->line);
return DatabaseGlobalInit(loop, listener, block, error);
return DatabaseGlobalInit(loop, listener, block);
} else {
/* if there is no override, use the cache directory */
@ -63,6 +58,6 @@ CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener,
ConfigBlock block;
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 DatabaseListener;
class Database;
class Error;
/**
* Read database configuration settings and create a #Database
* instance from it, but do not open it. Returns nullptr on error or
* if no database is configured (no #Error set in that case).
* instance from it, but do not open it. Returns nullptr if no
* database is configured.
*
* Throws #std::runtime_error on error.
*/
Database *
CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener,
Error &error);
CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener);
#endif

View File

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

View File

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