Skip to content

Commit

Permalink
More of: Log errors instead of throw
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Heinrich <andreas.heinrich@rwth-aachen.de>
  • Loading branch information
andistorm committed Jun 12, 2024
1 parent 6f133a1 commit e77bbde
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
24 changes: 16 additions & 8 deletions lib/error/error_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,56 +93,64 @@ ErrorFilter::ErrorFilter(const FilterVariant& filter_) : filter(filter_) {

FilterType ErrorFilter::get_filter_type() const {
if (filter.index() == 0) {
throw EverestBaseLogicError("Filter type is not set.");
EVLOG_error << "Filter type is not set. Defaulting to 'FilterType::State'.";
return FilterType::State;
}
return static_cast<FilterType>(filter.index());
}

StateFilter ErrorFilter::get_state_filter() const {
if (this->get_filter_type() != FilterType::State) {
throw EverestBaseLogicError("Filter type is not StateFilter.");
EVLOG_error << "Filter type is not StateFilter. Defaulting to 'StateFilter::Active'.";
return StateFilter::Active;
}
return std::get<StateFilter>(filter);
}

OriginFilter ErrorFilter::get_origin_filter() const {
if (this->get_filter_type() != FilterType::Origin) {
throw EverestBaseLogicError("Filter type is not OriginFilter.");
EVLOG_error << "Filter type is not OriginFilter. Defaulting to 'OriginFilter::ImplementationIdentifier(\"no-module-id-provided\", \"no-implementation-id-provided\")'.";
return OriginFilter(ImplementationIdentifier("no-module-id-provided", "no-implementation-id-provided"));
}
return std::get<OriginFilter>(filter);
}

TypeFilter ErrorFilter::get_type_filter() const {
if (this->get_filter_type() != FilterType::Type) {
throw EverestBaseLogicError("Filter type is not TypeFilter.");
EVLOG_error << "Filter type is not TypeFilter. Defaulting to 'TypeFilter(\"no-type-provided\")'.";
return TypeFilter("no-type-provided");
}
return std::get<TypeFilter>(filter);
}

SeverityFilter ErrorFilter::get_severity_filter() const {
if (this->get_filter_type() != FilterType::Severity) {
throw EverestBaseLogicError("Filter type is not SeverityFilter.");
EVLOG_error << "Filter type is not SeverityFilter. Defaulting to 'SeverityFilter::HIGH_GE'.";
return SeverityFilter::HIGH_GE;
}
return std::get<SeverityFilter>(filter);
}

TimePeriodFilter ErrorFilter::get_time_period_filter() const {
if (this->get_filter_type() != FilterType::TimePeriod) {
throw EverestBaseLogicError("Filter type is not TimePeriodFilter.");
EVLOG_error << "Filter type is not TimePeriodFilter. Defaulting to 'TimePeriodFilter{Error::time_point(), Error::time_point()}'.";
return TimePeriodFilter{Error::time_point(), Error::time_point()};
}
return std::get<TimePeriodFilter>(filter);
}

HandleFilter ErrorFilter::get_handle_filter() const {
if (this->get_filter_type() != FilterType::Handle) {
throw EverestBaseLogicError("Filter type is not HandleFilter.");
EVLOG_error << "Filter type is not HandleFilter. Defaulting to 'HandleFilter()'.";
return HandleFilter();
}
return std::get<HandleFilter>(filter);
}

SubTypeFilter ErrorFilter::get_sub_type_filter() const {
if (this->get_filter_type() != FilterType::SubType) {
throw EverestBaseLogicError("Filter type is not SubTypeFilter.");
EVLOG_error << "Filter type is not SubTypeFilter. Defaulting to 'SubTypeFilter(\"no-sub-type-provided\")'.";
return SubTypeFilter("no-sub-type-provided");
}
return std::get<SubTypeFilter>(filter);
}
Expand Down
25 changes: 13 additions & 12 deletions lib/error/error_type_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void ErrorTypeMap::load_error_types(std::filesystem::path error_types_dir) {
BOOST_LOG_FUNCTION();

if (!std::filesystem::is_directory(error_types_dir) || !std::filesystem::exists(error_types_dir)) {
throw EverestDirectoryNotFoundError(
fmt::format("Error types directory '{}' does not exist.", error_types_dir.string()));
EVLOG_error << "Error types directory '" << error_types_dir.string() << "' does not exist, error types not loaded.";
return;
}
for (const auto& entry : std::filesystem::directory_iterator(error_types_dir)) {
if (!entry.is_regular_file()) {
Expand All @@ -36,25 +36,26 @@ void ErrorTypeMap::load_error_types(std::filesystem::path error_types_dir) {
continue;
}
if (!error_type_file.at("errors").is_array()) {
throw EverestParseError(fmt::format("Error type file '{}' does not contain an array with key 'errors'.",
entry.path().string()));
EVLOG_error << "Error type file '" << entry.path().string() << "' does not contain an array with key 'errors', skipped.";
continue;
}
for (const auto& error : error_type_file["errors"]) {
if (!error.contains("name")) {
throw EverestParseError(
fmt::format("Error type file '{}' contains an error without a 'name' key.", entry.path().string()));
EVLOG_error << "Error type file '" << entry.path().string() << "' contains an error without a 'name' key, skipped.";
continue;
}
std::description description;
if (!error.contains("description")) {
throw EverestParseError(fmt::format(
"Error type file '{}' contains an error without a 'description' key.", entry.path().string()));
EVLOG_error << "Error type file '" << entry.path().string() << "' contains an error without a 'description' key, using default description";
description = "No description found";
} else {
description = error.at("description").get<std::string>();
}
ErrorType complete_name = prefix + "/" + error.at("name").get<std::string>();
if (this->has(complete_name)) {
throw EverestAlreadyExistsError(
fmt::format("Error type file '{}' contains an error with the name '{}' which is already defined.",
entry.path().string(), complete_name));
EVLOG_error << "Error type file '" << entry.path().string() << "' contains an error with the name '" << complete_name << "' which is already defined, skipped.";
continue;
}
std::string description = error.at("description").get<std::string>();
error_types[complete_name] = description;
}
}
Expand Down
15 changes: 9 additions & 6 deletions lib/everest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,17 @@ void Everest::subscribe_error(const Requirement& req, const error::ErrorType& er
// split error_type at '/'
int pos = error_type.find('/');
if (pos == std::string::npos) {
throw error::EverestNotValidErrorTypeError(error_type);
EVLOG_error << fmt::format("Error type {} is not valid, ignore subscription", error_type);
return;
}
std::string error_type_namespace = error_type.substr(0, pos);
std::string error_type_name = error_type.substr(pos + 1);
if (!requirement_impl_if.contains("errors") || !requirement_impl_if.at("errors").contains(error_type_namespace) ||
!requirement_impl_if.at("errors").at(error_type_namespace).contains(error_type_name)) {
throw error::EverestInterfaceMissingDeclartionError(
fmt::format("{}: Error {} not listed in interface!",
this->config.printable_identifier(requirement_module_id, requirement_impl_id), error_type));
EVLOG_error << fmt::format(
"{}: Error {} not listed in interface, ignore subscription!",
this->config.printable_identifier(requirement_module_id, requirement_impl_id), error_type);
return;
}

Handler raise_handler = [this, requirement_module_id, requirement_impl_id, error_type, callback](json const& data) {
Expand Down Expand Up @@ -554,8 +556,9 @@ void Everest::subscribe_global_all_errors(const error::ErrorCallback& callback,
EVLOG_debug << fmt::format("subscribing to all errors");

if (not this->config.get_module_info(this->module_id).global_errors_enabled) {
throw error::EverestNotAllowedError(fmt::format("Module {} is not allowed to subscribe to all errors!",
this->config.printable_identifier(this->module_id)));
EVLOG_error << fmt::format("Module {} is not allowed to subscribe to all errors, ignore subscription",
this->config.printable_identifier(this->module_id));
return;
}

Handler raise_handler = [this, callback](json const& data) {
Expand Down

0 comments on commit e77bbde

Please sign in to comment.