summaryrefslogtreecommitdiff
path: root/src/config
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2016-11-07 09:07:50 +0100
committerMax Kellermann <max@musicpd.org>2016-11-07 09:07:50 +0100
commit4aab97ccb140cb098e93de4d67944e2df07c6af7 (patch)
tree1eaf4daa95c25aec9fa60195d7a6812b60bf7ffb /src/config
parent4cd21f1e072d4219b44a54d5e6e62c6294c9a236 (diff)
config/Path: throw std::runtime_error on error
Diffstat (limited to 'src/config')
-rw-r--r--src/config/Block.cxx11
-rw-r--r--src/config/Block.hxx1
-rw-r--r--src/config/ConfigPath.cxx44
-rw-r--r--src/config/ConfigPath.hxx6
-rw-r--r--src/config/Param.cxx24
-rw-r--r--src/config/Param.hxx7
6 files changed, 30 insertions, 63 deletions
diff --git a/src/config/Block.cxx b/src/config/Block.cxx
index 96b91ee18..b9eaf3ebc 100644
--- a/src/config/Block.cxx
+++ b/src/config/Block.cxx
@@ -24,7 +24,6 @@
#include "system/FatalError.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
-#include "util/Error.hxx"
#include <assert.h>
#include <stdlib.h>
@@ -94,12 +93,10 @@ ConfigBlock::GetBlockValue(const char *name, const char *default_value) const
AllocatedPath
ConfigBlock::GetPath(const char *name, const char *default_value) const
{
- int line2 = line;
const char *s;
const BlockParam *bp = GetBlockParam(name);
if (bp != nullptr) {
- line2 = bp->line;
s = bp->value.c_str();
} else {
if (default_value == nullptr)
@@ -108,13 +105,7 @@ ConfigBlock::GetPath(const char *name, const char *default_value) const
s = default_value;
}
- Error error;
- AllocatedPath path = ParsePath(s, error);
- if (gcc_unlikely(path.IsNull()))
- throw FormatRuntimeError("Invalid path in \"%s\" at line %i: %s",
- name, line2, error.GetMessage());
-
- return path;
+ return ParsePath(s);
}
int
diff --git a/src/config/Block.hxx b/src/config/Block.hxx
index 7dfd123cc..b04388797 100644
--- a/src/config/Block.hxx
+++ b/src/config/Block.hxx
@@ -27,7 +27,6 @@
#include <string>
#include <vector>
-class Error;
class AllocatedPath;
struct BlockParam {
diff --git a/src/config/ConfigPath.cxx b/src/config/ConfigPath.cxx
index ee5a8cd7b..eda7750ef 100644
--- a/src/config/ConfigPath.cxx
+++ b/src/config/ConfigPath.cxx
@@ -23,7 +23,7 @@
#include "fs/Traits.hxx"
#include "fs/Domain.hxx"
#include "fs/StandardDirectory.hxx"
-#include "util/Error.hxx"
+#include "util/RuntimeError.hxx"
#include "ConfigGlobal.hxx"
#include <assert.h>
@@ -36,14 +36,11 @@
* Determine a given user's home directory.
*/
static AllocatedPath
-GetHome(const char *user, Error &error)
+GetHome(const char *user)
{
AllocatedPath result = GetHomeDir(user);
- if (result.IsNull()) {
- error.Format(path_domain,
- "no such user: %s", user);
- return AllocatedPath::Null();
- }
+ if (result.IsNull())
+ throw FormatRuntimeError("no such user: %s", user);
return result;
}
@@ -52,34 +49,33 @@ GetHome(const char *user, Error &error)
* Determine the current user's home directory.
*/
static AllocatedPath
-GetHome(Error &error)
+GetHome()
{
AllocatedPath result = GetHomeDir();
- if (result.IsNull()) {
- error.Set(path_domain,
- "problems getting home for current user");
- return AllocatedPath::Null();
- }
+ if (result.IsNull())
+ throw std::runtime_error("problems getting home for current user");
return result;
}
/**
* Determine the configured user's home directory.
+ *
+ * Throws #std::runtime_error on error.
*/
static AllocatedPath
-GetConfiguredHome(Error &error)
+GetConfiguredHome()
{
const char *user = config_get_string(ConfigOption::USER);
return user != nullptr
- ? GetHome(user, error)
- : GetHome(error);
+ ? GetHome(user)
+ : GetHome();
}
#endif
AllocatedPath
-ParsePath(const char *path, Error &error)
+ParsePath(const char *path)
{
assert(path != nullptr);
@@ -88,12 +84,12 @@ ParsePath(const char *path, Error &error)
++path;
if (*path == '\0')
- return GetConfiguredHome(error);
+ return GetConfiguredHome();
AllocatedPath home = AllocatedPath::Null();
if (*path == '/') {
- home = GetConfiguredHome(error);
+ home = GetConfiguredHome();
++path;
} else {
@@ -102,7 +98,7 @@ ParsePath(const char *path, Error &error)
? path + strlen(path)
: slash;
const std::string user(path, end);
- home = GetHome(user.c_str(), error);
+ home = GetHome(user.c_str());
if (slash == nullptr)
return home;
@@ -113,18 +109,16 @@ ParsePath(const char *path, Error &error)
if (home.IsNull())
return AllocatedPath::Null();
- AllocatedPath path2 = AllocatedPath::FromUTF8(path, error);
+ AllocatedPath path2 = AllocatedPath::FromUTF8Throw(path);
if (path2.IsNull())
return AllocatedPath::Null();
return AllocatedPath::Build(home, path2);
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
- error.Format(path_domain,
- "not an absolute path: %s", path);
- return AllocatedPath::Null();
+ throw FormatRuntimeError("not an absolute path: %s", path);
} else {
#endif
- return AllocatedPath::FromUTF8(path, error);
+ return AllocatedPath::FromUTF8Throw(path);
#ifndef WIN32
}
#endif
diff --git a/src/config/ConfigPath.hxx b/src/config/ConfigPath.hxx
index 340145e48..a04f3a112 100644
--- a/src/config/ConfigPath.hxx
+++ b/src/config/ConfigPath.hxx
@@ -21,9 +21,11 @@
#define MPD_CONFIG_PATH_HXX
class AllocatedPath;
-class Error;
+/**
+ * Throws #std::runtime_error on error.
+ */
AllocatedPath
-ParsePath(const char *path, Error &error);
+ParsePath(const char *path);
#endif
diff --git a/src/config/Param.cxx b/src/config/Param.cxx
index a3288c946..94ec2a70c 100644
--- a/src/config/Param.cxx
+++ b/src/config/Param.cxx
@@ -21,7 +21,7 @@
#include "Param.hxx"
#include "ConfigPath.hxx"
#include "fs/AllocatedPath.hxx"
-#include "util/Error.hxx"
+#include "util/RuntimeError.hxx"
#include <stdexcept>
@@ -34,24 +34,12 @@ ConfigParam::~ConfigParam()
}
AllocatedPath
-ConfigParam::GetPath(Error &error) const
-{
- auto path = ParsePath(value.c_str(), error);
- if (gcc_unlikely(path.IsNull()))
- error.FormatPrefix("Invalid path at line %i: ", line);
-
- return path;
-
-}
-
-AllocatedPath
ConfigParam::GetPath() const
{
- Error error;
- auto path = ParsePath(value.c_str(), error);
- if (gcc_unlikely(path.IsNull()))
- throw std::runtime_error(error.GetMessage());
-
- return path;
+ try {
+ return ParsePath(value.c_str());
+ } catch (...) {
+ std::throw_with_nested(FormatRuntimeError("Invalid path at line %i: ", line));
+ }
}
diff --git a/src/config/Param.hxx b/src/config/Param.hxx
index a1a5fccd2..50530292e 100644
--- a/src/config/Param.hxx
+++ b/src/config/Param.hxx
@@ -68,13 +68,6 @@ struct ConfigParam {
/**
* Parse the value as a path. If there is a tilde prefix, it
- * is expanded. If the path could not be parsed, returns
- * AllocatedPath::Null() and sets the error.
- */
- AllocatedPath GetPath(Error &error) const;
-
- /**
- * Parse the value as a path. If there is a tilde prefix, it
* is expanded.
*
* Throws #std::runtime_error on error.