Skip to content

Commit

Permalink
Refactor error handling internally
Browse files Browse the repository at this point in the history
* Use std::optional<std::string> with nullopt instead of empty string for reuqest_clear_error function
* Move error related files in to subdirectory
* Remove error attribute persistent
* Add error attribute state (Active/ClearedByModule/ClearedByReboot)
* Add error_exceptions.hpp/cpp
* Add error_type_map.hpp/cpp
* Add error_json.cpp
* Rename error_manager -> error_comm_bridge
* Add error_manager that is called by error_comm_bridge
* Rename error_database -> error_database_map
* Add abstract class error_database

Signed-off-by: Andreas Heinrich <[email protected]>

Minor suggestions in PR:

* Update include/utils/error/error_exceptions.hpp
* Update lib/error/error_database_map.cpp
* Update lib/error/error_type_map.cpp
* Update lib/error/error_type_map.cpp
* Update lib/everest.cpp

Co-authored-by: Kai Hermann <[email protected]>
Signed-off-by: Andreas Heinrich <[email protected]>
  • Loading branch information
andistorm and hikinggrass committed Dec 6, 2023
1 parent a98d614 commit b3a5dad
Show file tree
Hide file tree
Showing 32 changed files with 1,121 additions and 657 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ genrule(

cc_library(
name = "framework",
srcs = glob(["lib/*.cpp"]),
srcs = glob(["lib/**/*.cpp"]),
hdrs = glob(["include/**/*.hpp"]) + [":compile_time_settings"],
copts = ["-std=c++17"],
strip_include_prefix = "include",
Expand Down
15 changes: 8 additions & 7 deletions everestjs/everestjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <future>
#include <iostream>
#include <map>
#include <optional>
#include <thread>
#include <vector>

Expand Down Expand Up @@ -359,8 +360,8 @@ static Napi::Value subscribe_error_cleared(const Requirement& req, const std::st
}

static Napi::Value request_clear_error(const Everest::error::RequestClearErrorOption request_type,
const std::string& impl_id, const std::string& uuid,
const std::string& error_type, const Napi::Env& env) {
const std::string& impl_id, const std::optional<std::string>& uuid,
const std::optional<std::string>& error_type, const Napi::Env& env) {
BOOST_LOG_FUNCTION();

Napi::Value result;
Expand Down Expand Up @@ -498,7 +499,7 @@ static Napi::Value boot_module(const Napi::CallbackInfo& info) {
[impl_id, error_type](const Napi::CallbackInfo& info) {
return request_clear_error(
Everest::error::RequestClearErrorOption::ClearAllOfTypeOfModule,
impl_id, "", error_type, info.Env());
impl_id, std::nullopt, error_type, info.Env());
}),
napi_enumerable));
}
Expand All @@ -514,8 +515,8 @@ static Napi::Value boot_module(const Napi::CallbackInfo& info) {
[impl_id](const Napi::CallbackInfo& info) {
const std::string uuid = info[0].ToString().Utf8Value();
return request_clear_error(
Everest::error::RequestClearErrorOption::ClearUUID, impl_id, uuid, "",
info.Env());
Everest::error::RequestClearErrorOption::ClearUUID, impl_id, uuid,
std::nullopt, info.Env());
}),
napi_enumerable));

Expand All @@ -524,8 +525,8 @@ static Napi::Value boot_module(const Napi::CallbackInfo& info) {
Napi::Function::New(env,
[impl_id](const Napi::CallbackInfo& info) {
return request_clear_error(
Everest::error::RequestClearErrorOption::ClearAllOfModule, impl_id, "",
"", info.Env());
Everest::error::RequestClearErrorOption::ClearAllOfModule, impl_id,
std::nullopt, std::nullopt, info.Env());
}),
napi_enumerable));
}
Expand Down
8 changes: 4 additions & 4 deletions everestpy/src/everest/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,21 @@ std::string Module::raise_error(const std::string& impl_id, const std::string& e
json Module::request_clear_error_uuid(const std::string& impl_id, const std::string& uuid) {
pybind11::gil_scoped_release release;
const auto& result =
handle->request_clear_error(Everest::error::RequestClearErrorOption::ClearUUID, impl_id, uuid, "");
handle->request_clear_error(Everest::error::RequestClearErrorOption::ClearUUID, impl_id, uuid, std::nullopt);
return result;
}

json Module::request_clear_error_all_of_type(const std::string& impl_id, const std::string& error_type) {
pybind11::gil_scoped_release release;
const auto& result = handle->request_clear_error(Everest::error::RequestClearErrorOption::ClearAllOfTypeOfModule,
impl_id, "", error_type);
impl_id, std::nullopt, error_type);
return result;
}

json Module::request_clear_error_all_of_module(const std::string& impl_id) {
pybind11::gil_scoped_release release;
const auto& result =
handle->request_clear_error(Everest::error::RequestClearErrorOption::ClearAllOfModule, impl_id, "", "");
const auto& result = handle->request_clear_error(Everest::error::RequestClearErrorOption::ClearAllOfModule, impl_id,
std::nullopt, std::nullopt);
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion include/framework/ModuleAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <everest/logging.hpp>
#include <utils/conversions.hpp>
#include <utils/date.hpp>
#include <utils/error.hpp>
#include <utils/error/error.hpp>

#include <iomanip>
#include <iostream>
Expand Down
5 changes: 3 additions & 2 deletions include/framework/everest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include <everest/exceptions.hpp>

#include <utils/config.hpp>
#include <utils/error.hpp>
#include <utils/error/error.hpp>
#include <utils/error/error_manager.hpp>
#include <utils/mqtt_abstraction.hpp>
#include <utils/types.hpp>

Expand Down Expand Up @@ -105,7 +106,7 @@ class Everest {
/// of the cleared errors
///
json request_clear_error(const error::RequestClearErrorOption request_type, const std::string& impl_id,
const std::string& uuid, const std::string& error_type);
const std::optional<std::string>& uuid, const std::optional<std::string>& error_type);

///
/// \brief Raises an given \p error of the given \p impl_id, with the given \p error_type. Returns the uuid of the
Expand Down
3 changes: 2 additions & 1 deletion include/utils/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#include <nlohmann/json-schema.hpp>

#include <utils/config_cache.hpp>
#include <utils/error.hpp>
#include <utils/error/error.hpp>
#include <utils/error/error_type_map.hpp>
#include <utils/types.hpp>

namespace Everest {
Expand Down
94 changes: 0 additions & 94 deletions include/utils/error.hpp

This file was deleted.

73 changes: 73 additions & 0 deletions include/utils/error/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest
#ifndef UTILS_ERROR_HPP
#define UTILS_ERROR_HPP

#include <string>

#include <utils/date.hpp>
#include <utils/types.hpp>

namespace Everest {
namespace error {

enum class Severity {
Low,
Medium,
High
};
std::string severity_to_string(const Severity& s);
Severity string_to_severity(const std::string& s);

struct UUID {
UUID();
explicit UUID(const std::string& uuid);
bool operator<(const UUID& other) const;
bool operator==(const UUID& other) const;
bool operator!=(const UUID& other) const;
std::string to_string() const;

std::string uuid;
};

using ErrorType = std::string;

enum class State {
Active,
ClearedByModule,
ClearedByReboot
};
std::string state_to_string(const State& s);
State string_to_state(const std::string& s);

///
/// \brief The Error struct represents an error object
///
struct Error {
using time_point = date::utc_clock::time_point;
Error(const ErrorType& type, const std::string& message, const std::string& description,
const ImplementationIdentifier& from, const Severity& severity, const time_point& timestamp, const UUID& uuid,
const State& state = State::Active);
Error(const ErrorType& type, const std::string& message, const std::string& description,
const ImplementationIdentifier& from, const Severity& severity = Severity::Low);
Error(const ErrorType& type, const std::string& message, const std::string& description,
const std::string& from_module, const std::string& from_implementation,
const Severity& severity = Severity::Low);
ErrorType type;
std::string description;
std::string message;
Severity severity;
ImplementationIdentifier from;
time_point timestamp;
UUID uuid;
State state;
};

using ErrorHandle = UUID;
using ErrorPtr = std::shared_ptr<Error>;
using ErrorCallback = std::function<void(Error)>;

} // namespace error
} // namespace Everest

#endif // UTILS_ERROR_HPP
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
#ifndef UTILS_ERROR_MANAGER_HPP
#define UTILS_ERROR_MANAGER_HPP
// Copyright Pionix GmbH and Contributors to EVerest

#include <functional>
#ifndef UTILS_ERROR_COMM_BRIDGE_HPP
#define UTILS_ERROR_COMM_BRIDGE_HPP

#include <utils/error_database.hpp>
#include <functional>
#include <utils/error/error_manager.hpp>
#include <utils/types.hpp>

namespace Everest {
namespace error {

class ErrorManager {
class ErrorCommBridge {
public:
using HandlerFunc = std::function<void(const json&)>;
using SendMessageFunc = std::function<void(const std::string&, const json&)>;
using RegisterHandlerFunc = std::function<void(const std::string&, HandlerFunc&)>;
using RegisterCallHandlerFunc = RegisterHandlerFunc;
using RegisterErrorHandlerFunc = RegisterHandlerFunc;

public:
void add_error_topic(const std::string& topic);
ErrorManager(SendMessageFunc send_json_message_, RegisterCallHandlerFunc register_call_handler_,
RegisterErrorHandlerFunc register_error_handler_, const std::string& request_clear_error_topic_);
ErrorCommBridge(std::shared_ptr<ErrorManager> error_manager_, SendMessageFunc send_json_message_,
RegisterCallHandlerFunc register_call_handler_, RegisterErrorHandlerFunc register_error_handler_,
const std::string& request_clear_error_topic_);

private:
void handle_error(const json& data);
void handle_request_clear_error(const json& data);

ErrorDatabase error_database;
std::shared_ptr<ErrorManager> error_manager;
std::string request_clear_error_topic;

RegisterCallHandlerFunc register_call_handler;
Expand All @@ -39,4 +39,4 @@ class ErrorManager {
} // namespace error
} // namespace Everest

#endif // UTILS_ERROR_MANAGER_HPP
#endif // UTILS_ERROR_COMM_BRIDGE_HPP
30 changes: 30 additions & 0 deletions include/utils/error/error_database.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#ifndef UTILS_ERROR_DATABASE_HPP
#define UTILS_ERROR_DATABASE_HPP

#include <list>
#include <memory>

#include <utils/error/error_filter.hpp>

namespace Everest {
namespace error {

class ErrorDatabase {
public:
using EditErrorFunc = std::function<void(ErrorPtr)>;

ErrorDatabase() = default;

virtual void add_error(ErrorPtr error) = 0;
virtual std::list<ErrorPtr> get_errors(const std::list<ErrorFilter>& filters) const = 0;
virtual std::list<ErrorPtr> edit_errors(const std::list<ErrorFilter>& filters, EditErrorFunc edit_func) = 0;
virtual std::list<ErrorPtr> remove_errors(const std::list<ErrorFilter>& filters) = 0;
};

} // namespace error
} // namespace Everest

#endif // UTILS_ERROR_DATABASE_HPP
Loading

0 comments on commit b3a5dad

Please sign in to comment.