Skip to content
Snippets Groups Projects
Commit e78c6a69 authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

use c++ templates for all errors

parent 45be8cb4
No related branches found
No related tags found
2 merge requests!32Feature worker perf,!93Release 20.03.01
......@@ -10,7 +10,7 @@ namespace asapo {
enum class ErrorType {
kUnknownError,
kHidraError,
kAsapoError,
kHttpError,
kIOError,
kReceiverError,
......@@ -46,8 +46,8 @@ class ErrorTemplateInterface {
public:
virtual ErrorType GetErrorType() const noexcept = 0;
virtual Error Generate() const noexcept = 0;
virtual Error Generate(const std::string& suffix) const noexcept = 0;
virtual std::string Text() const noexcept = 0;
virtual inline bool operator == (const Error& rhs) const {
return rhs != nullptr &&
GetErrorType() == rhs->GetErrorType();
......@@ -79,7 +79,7 @@ static inline std::ostream& operator<<(std::ostream& os, const Error& err) {
class SimpleError: public ErrorInterface {
private:
std::string error_;
ErrorType error_type_ = ErrorType::kHidraError;
ErrorType error_type_ = ErrorType::kAsapoError;
public:
explicit SimpleError(std::string error): error_{std::move(error)} {
......@@ -109,7 +109,7 @@ class SimpleError: public ErrorInterface {
class SimpleErrorTemplate : public ErrorTemplateInterface {
protected:
std::string error_;
ErrorType error_type_ = ErrorType::kHidraError;
ErrorType error_type_ = ErrorType::kAsapoError;
public:
explicit SimpleErrorTemplate(std::string error): error_{std::move(error)} {
......@@ -130,6 +130,11 @@ class SimpleErrorTemplate : public ErrorTemplateInterface {
inline Error Generate() const noexcept override {
return Error(new SimpleError{error_, error_type_});
}
inline Error Generate(const std::string& suffix) const noexcept override {
return Error(new SimpleError{error_ + " :" + suffix, error_type_});
}
};
static inline std::ostream& operator<<(std::ostream& os, const SimpleErrorTemplate& err) {
......@@ -157,45 +162,44 @@ auto const kEndOfFile = SimpleErrorTemplate {
template <typename ServiceErrorType, ErrorType MainErrorType>
class ServiceError : public SimpleError {
private:
ServiceErrorType error_type_;
public:
ServiceError(const std::string& error, ServiceErrorType error_type) : SimpleError(error, MainErrorType) {
error_type_ = error_type;
}
ServiceErrorType GetServiceErrorType() const noexcept {
return error_type_;
}
private:
ServiceErrorType error_type_;
public:
ServiceError(const std::string& error, ServiceErrorType error_type) : SimpleError(error, MainErrorType) {
error_type_ = error_type;
}
ServiceErrorType GetServiceErrorType() const noexcept {
return error_type_;
}
};
template <typename ServiceErrorType, ErrorType MainErrorType>
class ServiceErrorTemplate : public SimpleErrorTemplate {
protected:
ServiceErrorType error_type_;
public:
ServiceErrorTemplate(const std::string& error, ServiceErrorType error_type) : SimpleErrorTemplate(error,
MainErrorType) {
error_type_ = error_type;
}
inline ServiceErrorType GetServiceErrorType() const noexcept {
return error_type_;
}
inline Error Generate() const noexcept override {
auto err = new ServiceError<ServiceErrorType, MainErrorType>(error_, error_type_);
return Error(err);
}
inline Error Generate(const std::string& prefix) const noexcept {
auto err = new ServiceError<ServiceErrorType, MainErrorType>(prefix + " :" + error_, error_type_);
return Error(err);
}
inline bool operator==(const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetServiceErrorType() == ((ServiceError<ServiceErrorType, MainErrorType>*) rhs.get())->GetServiceErrorType();
}
protected:
ServiceErrorType error_type_;
public:
ServiceErrorTemplate(const std::string& error, ServiceErrorType error_type) : SimpleErrorTemplate(error,
MainErrorType) {
error_type_ = error_type;
}
inline ServiceErrorType GetServiceErrorType() const noexcept {
return error_type_;
}
inline Error Generate() const noexcept override {
auto err = new ServiceError<ServiceErrorType, MainErrorType>(error_, error_type_);
return Error(err);
}
inline Error Generate(const std::string& suffix) const noexcept override {
return Error(new ServiceError<ServiceErrorType, MainErrorType>(error_ + " :" + suffix, error_type_));
}
inline bool operator==(const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetServiceErrorType() == ((ServiceError<ServiceErrorType, MainErrorType>*) rhs.get())->GetServiceErrorType();
}
};
}
......
......@@ -30,45 +30,8 @@ enum class IOErrorType {
};
class IOError : public SimpleError {
private:
IOErrorType io_error_type_;
public:
IOError(const std::string& error, IOErrorType io_error_type) : SimpleError(error, ErrorType::kIOError) {
io_error_type_ = io_error_type;
}
IOErrorType GetIOErrorType() const noexcept {
return io_error_type_;
}
};
class IOErrorTemplate : public SimpleErrorTemplate {
protected:
IOErrorType io_error_type_;
public:
IOErrorTemplate(const std::string& error, IOErrorType io_error_type) : SimpleErrorTemplate(error, ErrorType::kIOError) {
io_error_type_ = io_error_type;
}
inline IOErrorType GetIOErrorType() const noexcept {
return io_error_type_;
}
inline Error Generate() const noexcept override {
return Error(new IOError(error_, io_error_type_));
}
inline bool operator == (const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetIOErrorType() == ((IOError*)rhs.get())->GetIOErrorType();
}
};
static inline std::ostream& operator<<(std::ostream& os, const IOErrorTemplate& err) {
return os << err.Text();
}
using IOError = ServiceError<IOErrorType, ErrorType::kIOError>;
using IOErrorTemplate = ServiceErrorTemplate<IOErrorType, ErrorType::kIOError>;
namespace IOErrorTemplates {
auto const kUnknownIOError = IOErrorTemplate {
......
......@@ -19,48 +19,7 @@ enum class ProducerErrorType {
kRequestPoolIsFull
};
//TODO Make a marco to create error class and error template class
class ProducerError : public SimpleError {
private:
ProducerErrorType receiver_error_type_;
public:
ProducerError(const std::string& error, ProducerErrorType receiver_error_type) : SimpleError(error,
ErrorType::kProducerError) {
receiver_error_type_ = receiver_error_type;
}
ProducerErrorType GetProducerErrorType() const noexcept {
return receiver_error_type_;
}
};
class ProducerErrorTemplate : public SimpleErrorTemplate {
protected:
ProducerErrorType receiver_error_type_;
public:
ProducerErrorTemplate(const std::string& error, ProducerErrorType receiver_error_type) : SimpleErrorTemplate(error,
ErrorType::kProducerError) {
receiver_error_type_ = receiver_error_type;
}
inline ProducerErrorType GetProducerErrorType() const noexcept {
return receiver_error_type_;
}
inline Error Generate() const noexcept override {
return Error(new ProducerError(error_, receiver_error_type_));
}
inline bool operator==(const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetProducerErrorType() == ((ProducerError*) rhs.get())->GetProducerErrorType();
}
};
static inline std::ostream& operator<<(std::ostream& os, const ProducerErrorTemplate& err) {
return os << err.Text();
}
using ProducerErrorTemplate = ServiceErrorTemplate<ProducerErrorType, ErrorType::kProducerError>;
namespace ProducerErrorTemplates {
auto const kAlreadyConnected = ProducerErrorTemplate {
......
......@@ -10,52 +10,7 @@ enum class EventMonitorErrorType {
kSystemError
};
class EventMonitorError : public SimpleError {
private:
EventMonitorErrorType error_type_;
public:
EventMonitorError(const std::string& error, EventMonitorErrorType error_type) : SimpleError(error,
ErrorType::kHidraError) {
error_type_ = error_type;
}
EventMonitorErrorType GetEventMonitorErrorType() const noexcept {
return error_type_;
}
};
class EventMonitorErrorTemplate : public SimpleErrorTemplate {
protected:
EventMonitorErrorType error_type_;
public:
EventMonitorErrorTemplate(const std::string& error, EventMonitorErrorType error_type) : SimpleErrorTemplate(error,
ErrorType::kHidraError) {
error_type_ = error_type;
}
inline EventMonitorErrorType GetEventMonitorErrorType() const noexcept {
return error_type_;
}
inline Error Generate(std::string sub_error) const noexcept {
return Error(new EventMonitorError(error_ + ": " + sub_error, error_type_));
}
inline Error Generate() const noexcept override {
return Error(new EventMonitorError(error_, error_type_));
}
inline bool operator==(const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetEventMonitorErrorType() == ((EventMonitorError*) rhs.get())->GetEventMonitorErrorType();
}
};
static inline std::ostream& operator<<(std::ostream& os, const EventMonitorErrorTemplate& err) {
return os << err.Text();
}
using EventMonitorErrorTemplate = ServiceErrorTemplate<EventMonitorErrorType, ErrorType::kAsapoError>;
namespace EventMonitorErrorTemplates {
auto const kNoNewEvent = EventMonitorErrorTemplate {
......
......@@ -71,4 +71,4 @@ gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}" ${CMAKE_CURRENT_
set(TEST_SOURCE_FILES_RDS
unittests/receiver_data_server/test_receiver_data_server.cpp)
gtest(${TARGET_NAME}_RDS "${TEST_SOURCE_FILES_RDS}" "${TEST_LIBRARIES}")
gtest(${TARGET_NAME}_RDS "${TEST_SOURCE_FILES_RDS}" "${TEST_LIBRARIES}" ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h)
......@@ -11,58 +11,18 @@ enum class ReceiverErrorType {
kAuthorizationFailure
};
//TODO Make a marco to create error class and error template class
class ReceiverError : public SimpleError {
private:
ReceiverErrorType receiver_error_type_;
public:
ReceiverError(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleError(error,
ErrorType::kReceiverError) {
receiver_error_type_ = receiver_error_type;
}
ReceiverErrorType GetReceiverErrorType() const noexcept {
return receiver_error_type_;
}
};
class ReceiverErrorTemplate : public SimpleErrorTemplate {
protected:
ReceiverErrorType receiver_error_type_;
public:
ReceiverErrorTemplate(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleErrorTemplate(error,
ErrorType::kReceiverError) {
receiver_error_type_ = receiver_error_type;
}
inline ReceiverErrorType GetReceiverErrorType() const noexcept {
return receiver_error_type_;
}
inline Error Generate() const noexcept override {
return Error(new ReceiverError(error_, receiver_error_type_));
}
inline bool operator==(const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetReceiverErrorType() == ((ReceiverError*) rhs.get())->GetReceiverErrorType();
}
};
static inline std::ostream& operator<<(std::ostream& os, const ReceiverErrorTemplate& err) {
return os << err.Text();
}
using ReceiverErrorTemplate = ServiceErrorTemplate<ReceiverErrorType, ErrorType::kReceiverError>;
namespace ReceiverErrorTemplates {
auto const kInvalidOpCode = ReceiverErrorTemplate {
auto const kInvalidOpCode = ReceiverErrorTemplate{
"Invalid Opcode", ReceiverErrorType::kInvalidOpCode
};
auto const kBadRequest = ReceiverErrorTemplate {
auto const kBadRequest = ReceiverErrorTemplate{
"Bad request", ReceiverErrorType::kBadRequest
};
auto const kAuthorizationFailure = ReceiverErrorTemplate {
auto const kAuthorizationFailure = ReceiverErrorTemplate{
"authorization failure", ReceiverErrorType::kAuthorizationFailure
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment