From 78bf4ef5fa20cad2d91566ca82ad57de2dad0efb Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 28 Oct 2016 23:00:10 +0200 Subject: [PATCH] db/Configured: migrate from class Error to C++ exceptions --- src/Main.cxx | 11 +++-------- src/db/Configured.cxx | 21 ++++++++------------- src/db/Configured.hxx | 10 +++++----- src/db/DatabaseGlue.cxx | 12 +++++------- src/db/DatabaseGlue.hxx | 5 +++-- 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/Main.cxx b/src/Main.cxx index 1c3a3bcba..9a095b1c7 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -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(); diff --git a/src/db/Configured.cxx b/src/db/Configured.cxx index 42c1feccd..b667b8657 100644 --- a/src/db/Configured.cxx +++ b/src/db/Configured.cxx @@ -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); } } diff --git a/src/db/Configured.hxx b/src/db/Configured.hxx index a771d9242..43f9e736d 100644 --- a/src/db/Configured.hxx +++ b/src/db/Configured.hxx @@ -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 diff --git a/src/db/DatabaseGlue.cxx b/src/db/DatabaseGlue.cxx index 1cb526e49..82b1248a3 100644 --- a/src/db/DatabaseGlue.cxx +++ b/src/db/DatabaseGlue.cxx @@ -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); } diff --git a/src/db/DatabaseGlue.hxx b/src/db/DatabaseGlue.hxx index e1e0c4861..318f99960 100644 --- a/src/db/DatabaseGlue.hxx +++ b/src/db/DatabaseGlue.hxx @@ -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