Commit e70a83bd authored by Joseph Noir's avatar Joseph Noir

Add error handling for config file option

parent b49fc255
...@@ -342,7 +342,7 @@ private: ...@@ -342,7 +342,7 @@ private:
actor_system_config& set_impl(string_view name, config_value value); actor_system_config& set_impl(string_view name, config_value value);
void extract_config_file_path(string_list& args); error extract_config_file_path(string_list& args);
/// Adjusts the content of the configuration, e.g., for ensuring backwards /// Adjusts the content of the configuration, e.g., for ensuring backwards
/// compatibility with older options. /// compatibility with older options.
......
...@@ -114,6 +114,8 @@ enum class sec : uint8_t { ...@@ -114,6 +114,8 @@ enum class sec : uint8_t {
bad_function_call = 40, bad_function_call = 40,
/// Feature is disabled in the actor system config. /// Feature is disabled in the actor system config.
feature_disabled, feature_disabled,
/// Failed to open file.
cannot_open_file,
}; };
/// @relates sec /// @relates sec
......
...@@ -35,6 +35,12 @@ ...@@ -35,6 +35,12 @@
namespace caf { namespace caf {
namespace {
constexpr const char* default_config_file = "caf-application.ini";
} // namespace anonymous
actor_system_config::~actor_system_config() { actor_system_config::~actor_system_config() {
// nop // nop
} }
...@@ -45,7 +51,7 @@ actor_system_config::~actor_system_config() { ...@@ -45,7 +51,7 @@ actor_system_config::~actor_system_config() {
actor_system_config::actor_system_config() actor_system_config::actor_system_config()
: cli_helptext_printed(false), : cli_helptext_printed(false),
slave_mode(false), slave_mode(false),
config_file_path("caf-application.ini"), config_file_path(default_config_file),
slave_mode_fun(nullptr) { slave_mode_fun(nullptr) {
// add `vector<T>` and `stream<T>` for each statically known type // add `vector<T>` and `stream<T>` for each statically known type
add_message_type_impl<stream<actor>>("stream<@actor>"); add_message_type_impl<stream<actor>>("stream<@actor>");
...@@ -287,8 +293,11 @@ error actor_system_config::parse(string_list args, const char* ini_file_cstr) { ...@@ -287,8 +293,11 @@ error actor_system_config::parse(string_list args, const char* ini_file_cstr) {
if (ini_file_cstr != nullptr) if (ini_file_cstr != nullptr)
config_file_path = ini_file_cstr; config_file_path = ini_file_cstr;
// CLI arguments always win. // CLI arguments always win.
extract_config_file_path(args); if (auto err = extract_config_file_path(args))
return err;
std::ifstream ini{config_file_path}; std::ifstream ini{config_file_path};
if (config_file_path != default_config_file && !ini)
return make_error(sec::cannot_open_file, config_file_path);
return parse(std::move(args), ini); return parse(std::move(args), ini);
} }
...@@ -367,22 +376,23 @@ std::string actor_system_config::render_pec(uint8_t x, atom_value, ...@@ -367,22 +376,23 @@ std::string actor_system_config::render_pec(uint8_t x, atom_value,
meta::omittable_if_empty(), xs); meta::omittable_if_empty(), xs);
} }
void actor_system_config::extract_config_file_path(string_list& args) { error actor_system_config::extract_config_file_path(string_list& args) {
auto ptr = custom_options_.qualified_name_lookup("global.config-file"); auto ptr = custom_options_.qualified_name_lookup("global.config-file");
CAF_ASSERT(ptr != nullptr); CAF_ASSERT(ptr != nullptr);
string_list::iterator i; string_list::iterator i;
string_view path; string_view path;
std::tie(i, path) = find_by_long_name(*ptr, args.begin(), args.end()); std::tie(i, path) = find_by_long_name(*ptr, args.begin(), args.end());
if (i == args.end()) if (i == args.end())
return; return none;
if (path.empty()) { if (path.empty()) {
args.erase(i); args.erase(i);
return; return make_error(pec::missing_argument, std::string{*i});
} }
auto evalue = ptr->parse(path); auto evalue = ptr->parse(path);
if (!evalue) if (!evalue)
return; return std::move(evalue.error());
ptr->store(*evalue); ptr->store(*evalue);
return none;
} }
error actor_system_config::adjust_content() { error actor_system_config::adjust_content() {
......
...@@ -67,6 +67,7 @@ const char* sec_strings[] = { ...@@ -67,6 +67,7 @@ const char* sec_strings[] = {
"unhandled_stream_error", "unhandled_stream_error",
"bad_function_call", "bad_function_call",
"feature_disabled", "feature_disabled",
"cannot_open_file",
}; };
} // namespace <anonymous> } // namespace <anonymous>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment