return;
}
- std::string value = util::expand_environment_variables(unexpanded_value);
+ std::string value = util::value_or_throw<core::Error>(
+ util::expand_environment_variables(unexpanded_value));
switch (it->second.item) {
case ConfigItem::absolute_paths_in_stderr:
#include <Finalizer.hpp>
#include <Logging.hpp>
#include <Win32Util.hpp>
-#include <core/exceptions.hpp>
#include <fmtmacros.hpp>
#include <util/wincompat.hpp>
#include "fmtmacros.hpp"
#include <assertions.hpp>
-#include <core/exceptions.hpp>
#include <util/DirEntry.hpp>
#include <util/file.hpp>
#include <util/filesystem.hpp>
#include "environment.hpp"
-#include <core/exceptions.hpp>
#include <fmtmacros.hpp>
#include <util/wincompat.hpp>
namespace util {
-std::string
+tl::expected<std::string, std::string>
expand_environment_variables(const std::string& str)
{
std::string result;
++right;
}
if (curly && *right != '}') {
- throw core::Error(FMT("syntax error: missing '}}' after \"{}\"", left));
+ return tl::unexpected(
+ FMT("syntax error: missing '}}' after \"{}\"", left));
}
if (right == left) {
// Special case: don't consider a single $ the left of a variable.
std::string name(left, right - left);
const char* value = getenv(name.c_str());
if (!value) {
- throw core::Error(FMT("environment variable \"{}\" not set", name));
+ return tl::unexpected(
+ FMT("environment variable \"{}\" not set", name));
}
result += value;
if (!curly) {
#pragma once
+#include <third_party/tl/expected.hpp>
+
#include <string>
namespace util {
// Expand all instances of $VAR or ${VAR}, where VAR is an environment variable,
-// in `str`. Throws `core::Error` if one of the environment variables.
-[[nodiscard]] std::string expand_environment_variables(const std::string& str);
+// in `str`.
+tl::expected<std::string, std::string>
+expand_environment_variables(const std::string& str);
// Set environment variable `name` to `value`.
void setenv(const std::string& name, const std::string& value);
CHECK(util::expand_environment_variables("x$FOO") == "xbar");
CHECK(util::expand_environment_variables("${FOO}x") == "barx");
- DOCTEST_GCC_SUPPRESS_WARNING_PUSH
- DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-result")
- CHECK_THROWS_WITH(
- (void)util::expand_environment_variables("$surelydoesntexist"),
- "environment variable \"surelydoesntexist\" not set");
- CHECK_THROWS_WITH((void)util::expand_environment_variables("${FOO"),
- "syntax error: missing '}' after \"FOO\"");
- DOCTEST_GCC_SUPPRESS_WARNING_POP
+ CHECK(util::expand_environment_variables("$surelydoesntexist").error()
+ == "environment variable \"surelydoesntexist\" not set");
+ CHECK(util::expand_environment_variables("${FOO").error()
+ == "syntax error: missing '}' after \"FOO\"");
}
TEST_SUITE_END();