Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some more throws in error framework #194

Merged
merged 11 commits into from
Jun 14, 2024
53 changes: 0 additions & 53 deletions include/utils/error/error_exceptions.hpp

This file was deleted.

14 changes: 9 additions & 5 deletions lib/error/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>

#include <utils/error/error_exceptions.hpp>
#include <everest/logging.hpp>

namespace Everest {
namespace error {
Expand Down Expand Up @@ -76,7 +76,8 @@ std::string severity_to_string(const Severity& s) {
case Severity::High:
return "High";
}
throw std::out_of_range("No known string conversion for provided enum of type Severity.");
EVLOG_error << "No known string conversion for provided enum of type Severity. Defaulting to High.";
return "High";
}

Severity string_to_severity(const std::string& s) {
Expand All @@ -87,7 +88,8 @@ Severity string_to_severity(const std::string& s) {
} else if (s == "High") {
return Severity::High;
}
throw std::out_of_range("Provided string " + s + " could not be converted to enum of type Severity.");
EVLOG_error << "Provided string " << s << " could not be converted to enum of type Severity. Defaulting to High.";
return Severity::High;
}

std::string state_to_string(const State& s) {
Expand All @@ -99,7 +101,8 @@ std::string state_to_string(const State& s) {
case State::ClearedByReboot:
return "ClearedByReboot";
}
throw std::out_of_range("No known string conversion for provided enum of type State.");
EVLOG_error << "No known string conversion for provided enum of type State. Defaulting to Active.";
return "Active";
}

State string_to_state(const std::string& s) {
Expand All @@ -110,7 +113,8 @@ State string_to_state(const std::string& s) {
} else if (s == "ClearedByReboot") {
return State::ClearedByReboot;
}
throw std::out_of_range("Provided string " + s + " could not be converted to enum of type State.");
EVLOG_error << "Provided string " << s << " could not be converted to enum of type State. Defaulting to Active.";
return State::Active;
}

} // namespace error
Expand Down
11 changes: 6 additions & 5 deletions lib/error/error_database_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <everest/logging.hpp>
#include <utils/error.hpp>
#include <utils/error/error_exceptions.hpp>
#include <utils/error/error_json.hpp>

#include <algorithm>
Expand All @@ -16,8 +15,8 @@ namespace error {
void ErrorDatabaseMap::add_error(ErrorPtr error) {
std::lock_guard<std::mutex> lock(this->errors_mutex);
if (this->errors.find(error->uuid) != this->errors.end()) {
throw EverestAlreadyExistsError("Error with handle " + error->uuid.to_string() +
" already exists in ErrorDatabaseMap.");
EVLOG_error << "Error with handle " << error->uuid.to_string() << " already exists in ErrorDatabaseMap.";
return;
}
this->errors[error->uuid] = error;
}
Expand Down Expand Up @@ -60,7 +59,8 @@ std::list<ErrorPtr> ErrorDatabaseMap::get_errors_no_mutex(const std::list<ErrorF
return error->severity < Severity::High;
} break;
}
throw std::out_of_range("No known condition for provided enum of type SeverityFilter.");
EVLOG_error << "No known condition for provided enum of type SeverityFilter.";
return false;
};
} break;
case FilterType::TimePeriod: {
Expand All @@ -79,7 +79,8 @@ std::list<ErrorPtr> ErrorDatabaseMap::get_errors_no_mutex(const std::list<ErrorF
pred = [&filter](const ErrorPtr& error) { return error->vendor_id != filter.get_vendor_id_filter().value; };
} break;
default:
throw std::out_of_range("No known pred for provided enum of type FilterType.");
EVLOG_error << "No known pred for provided enum of type FilterType. Ignoring.";
return result;
}
result.remove_if(pred);
}
Expand Down
42 changes: 29 additions & 13 deletions lib/error/error_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#include <utils/error/error_exceptions.hpp>
#include <utils/error/error_filter.hpp>

#include <everest/exceptions.hpp>
#include <everest/logging.hpp>

namespace Everest {
namespace error {
Expand All @@ -26,7 +26,8 @@ std::string severity_filter_to_string(const SeverityFilter& f) {
case SeverityFilter::HIGH_GE:
return "HIGH_GE";
}
throw std::out_of_range("No known string conversion for provided enum of type SeverityFilter.");
EVLOG_error << "No known string conversion for provided enum of type SeverityFilter. Defaulting to HIGH_GE";
return "HIGH_GE";
}

SeverityFilter string_to_severity_filter(const std::string& s) {
Expand All @@ -37,7 +38,9 @@ SeverityFilter string_to_severity_filter(const std::string& s) {
} else if (s == "HIGH_GE") {
return SeverityFilter::HIGH_GE;
}
throw std::out_of_range("Provided string " + s + " could not be converted to enum of type SeverityFilter.");
EVLOG_error << "Provided string " << s
<< " could not be converted to enum of type SeverityFilter. Defaulting to HIGH_GE";
return SeverityFilter::HIGH_GE;
}

TypeFilter::TypeFilter(const ErrorType& value_) : value(value_) {
Expand Down Expand Up @@ -68,7 +71,8 @@ std::string filter_type_to_string(const FilterType& f) {
case FilterType::VendorId:
return "VendorId";
}
throw std::out_of_range("No known string conversion for provided enum of type FilterType.");
EVLOG_error << "No known string conversion for provided enum of type FilterType. Defaulting to Type";
return "Type";
}

FilterType string_to_filter_type(const std::string& s) {
Expand All @@ -89,7 +93,8 @@ FilterType string_to_filter_type(const std::string& s) {
} else if (s == "VendorId") {
return FilterType::VendorId;
}
throw std::out_of_range("Provided string " + s + " could not be converted to enum of type FilterType.");
EVLOG_error << "Provided string " << s << " could not be converted to enum of type FilterType. Deafulting to Type.";
return FilterType::Type;
}

ErrorFilter::ErrorFilter() = default;
Expand All @@ -99,56 +104,67 @@ 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
17 changes: 11 additions & 6 deletions lib/error/error_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <utils/error.hpp>
#include <utils/error/error_database.hpp>
#include <utils/error/error_exceptions.hpp>
#include <utils/error/error_type_map.hpp>

#include <everest/logging.hpp>
Expand All @@ -28,16 +27,21 @@ ErrorManagerImpl::ErrorManagerImpl(std::shared_ptr<ErrorTypeMap> error_type_map_
publish_raised_error(publish_raised_error_),
publish_cleared_error(publish_cleared_error_),
validate_error_types(validate_error_types_) {
if (validate_error_types) {
for (const ErrorType& type : allowed_error_types) {
if (!error_type_map->has(type)) {
EVLOG_error << "Error type '" << type << "' in allowed_error_types is not defined, ignored.";
}
}
}
}

void ErrorManagerImpl::raise_error(const Error& error) {
if (validate_error_types) {
if (std::find(allowed_error_types.begin(), allowed_error_types.end(), error.type) ==
allowed_error_types.end()) {
throw EverestArgumentError("Error type " + error.type + " is not allowed to be raised.");
}
if (!this->error_type_map->has(error.type)) {
throw EverestArgumentError("Error type " + error.type + " is not known.");
EVLOG_error << "Error type " << error.type << " is not allowed to be raised. Ignoring.";
return;
}
}
if (!can_be_raised(error.type, error.sub_type)) {
Expand Down Expand Up @@ -81,7 +85,8 @@ std::list<ErrorPtr> ErrorManagerImpl::clear_error(const ErrorType& type, const E
std::list<ErrorFilter> filters = {ErrorFilter(TypeFilter(type)), ErrorFilter(SubTypeFilter(sub_type))};
std::list<ErrorPtr> res = database->remove_errors(filters);
if (res.size() > 1) {
throw EverestBaseLogicError("There are more than one matching error, this is not valid");
EVLOG_error << "There are more than one matching error, this is not valid";
return {};
}
const ErrorPtr error = res.front();
error->state = State::ClearedByModule;
Expand Down
24 changes: 16 additions & 8 deletions lib/error/error_manager_req.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

#include <utils/error.hpp>
#include <utils/error/error_database.hpp>
#include <utils/error/error_exceptions.hpp>
#include <utils/error/error_filter.hpp>
#include <utils/error/error_type_map.hpp>

#include <everest/exceptions.hpp>
#include <everest/logging.hpp>

namespace Everest {
Expand All @@ -22,6 +20,12 @@ ErrorManagerReq::ErrorManagerReq(std::shared_ptr<ErrorTypeMap> error_type_map_,
database(error_database_),
allowed_error_types(allowed_error_types_),
subscribe_error_func(subscribe_error_func_) {

for (const ErrorType& type : allowed_error_types) {
if (!error_type_map->has(type)) {
EVLOG_error << "Error type '" << type << "' in allowed_error_types is not defined, ignored.";
}
}
ErrorCallback on_raise = [this](const Error& error) { this->on_error_raised(error); };
ErrorCallback on_clear = [this](const Error& error) { this->on_error_cleared(error); };
for (const ErrorType& type : allowed_error_types) {
Expand Down Expand Up @@ -54,11 +58,13 @@ void ErrorManagerReq::subscribe_all_errors(const ErrorCallback& callback, const

void ErrorManagerReq::on_error_raised(const Error& error) {
if (std::find(allowed_error_types.begin(), allowed_error_types.end(), error.type) == allowed_error_types.end()) {
throw EverestBaseLogicError("Error can't be raised by module_id " + error.origin.module_id +
" and implemenetation_id " + error.origin.implementation_id);
EVLOG_error << "Error can't be raised by module_id " << error.origin.module_id << " and implemenetation_id "
<< error.origin.implementation_id << ", ignored.";
return;
}
if (!error_type_map->has(error.type)) {
throw EverestBaseLogicError("Error type '" + error.type + "' is not defined");
EVLOG_error << "Error type '" << error.type << "' is not defined, ignored.";
return;
}
database->add_error(std::make_shared<Error>(error));
on_error(error, true);
Expand All @@ -68,11 +74,13 @@ void ErrorManagerReq::on_error_cleared(const Error& error) {
std::list<ErrorFilter> filters = {ErrorFilter(TypeFilter(error.type)), ErrorFilter(SubTypeFilter(error.sub_type))};
std::list<ErrorPtr> res = database->remove_errors(filters);
if (res.size() < 1) {
throw EverestBaseLogicError("Error wasn't raised, type: " + error.type + ", sub_type: " + error.sub_type);
EVLOG_error << "Error wasn't raised, type: " << error.type << ", sub_type: " << error.sub_type << ", ignored.";
return;
}
if (res.size() > 1) {
throw EverestBaseLogicError("More than one error is cleared, type: " + error.type +
", sub_type: " + error.sub_type);
EVLOG_error << "More than one error is cleared, type: " << error.type << ", sub_type: " << error.sub_type
<< ", ignored.";
return;
}

on_error(error, false);
Expand Down
Loading
Loading