From 4e0a7e18031c9292de441ef3796be77e1ca78dfd Mon Sep 17 00:00:00 2001 From: Alexander Damian Date: Mon, 6 Jan 2020 11:24:15 -0500 Subject: [PATCH] Added RdKafkaException base class --- include/cppkafka/exceptions.h | 35 ++++++++++++++------------- src/exceptions.cpp | 45 ++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/include/cppkafka/exceptions.h b/include/cppkafka/exceptions.h index c882b41b..a7ec1473 100644 --- a/include/cppkafka/exceptions.h +++ b/include/cppkafka/exceptions.h @@ -50,6 +50,19 @@ class CPPKAFKA_API Exception : public std::exception { std::string message_; }; +/** + * Base class for all rdkafka exceptions + */ +class CPPKAFKA_API RdKafkaException : public Exception { +public: + RdKafkaException(Error error); + RdKafkaException(Error error, std::string message); + + Error get_error() const noexcept; +private: + Error error_; +}; + /** * A configuration related error */ @@ -87,7 +100,7 @@ class CPPKAFKA_API ElementNotFound : public Exception { */ class CPPKAFKA_API ParseException : public Exception { public: - ParseException(const std::string& message); + ParseException(std::string message); }; /** @@ -101,37 +114,25 @@ class CPPKAFKA_API UnexpectedVersion : public Exception { /** * A generic rdkafka handle error */ -class CPPKAFKA_API HandleException : public Exception { +class CPPKAFKA_API HandleException : public RdKafkaException { public: HandleException(Error error); - - Error get_error() const; -private: - Error error_; }; /** * Consumer exception */ -class CPPKAFKA_API ConsumerException : public Exception { +class CPPKAFKA_API ConsumerException : public RdKafkaException { public: ConsumerException(Error error); - - Error get_error() const; -private: - Error error_; }; /** * Queue exception for rd_kafka_queue_t errors */ -class CPPKAFKA_API QueueException : public Exception { +class CPPKAFKA_API QueueException : public RdKafkaException { public: QueueException(Error error); - - Error get_error() const; -private: - Error error_; }; /** @@ -139,7 +140,7 @@ class CPPKAFKA_API QueueException : public Exception { */ class CPPKAFKA_API ActionTerminatedException : public Exception { public: - ActionTerminatedException(const std::string& error); + ActionTerminatedException(std::string message); }; } // cppkafka diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 3511b993..96007cc6 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -31,6 +31,7 @@ using std::string; using std::to_string; +using std::move; namespace cppkafka { @@ -45,6 +46,24 @@ const char* Exception::what() const noexcept { return message_.data(); } +// RdKafkaException + +RdKafkaException::RdKafkaException(Error error) +: Exception(error.to_string()) +, error_(move(error)) { + +} + +RdKafkaException::RdKafkaException(Error error, string message) +: Exception(move(message)) +, error_(move(error)) { + +} + +Error RdKafkaException::get_error() const noexcept { + return error_; +} + // ConfigException ConfigException::ConfigException(const string& config_name, const string& error) @@ -75,8 +94,8 @@ ElementNotFound::ElementNotFound(const string& element_type, const string& name) // ParseException -ParseException::ParseException(const string& message) -: Exception(message) { +ParseException::ParseException(string message) +: Exception(move(message)) { } @@ -89,40 +108,28 @@ UnexpectedVersion::UnexpectedVersion(uint32_t version) // HandleException HandleException::HandleException(Error error) -: Exception(error.to_string()), error_(error) { +: RdKafkaException(move(error)) { } -Error HandleException::get_error() const { - return error_; -} - // ConsumerException ConsumerException::ConsumerException(Error error) -: Exception(error.to_string()), error_(error) { +: RdKafkaException(move(error)) { } -Error ConsumerException::get_error() const { - return error_; -} - // QueueException QueueException::QueueException(Error error) -: Exception(error.to_string()), error_(error) { - -} +: RdKafkaException(move(error)) { -Error QueueException::get_error() const { - return error_; } // ActionTerminatedException -ActionTerminatedException::ActionTerminatedException(const string& error) -: Exception(error) { +ActionTerminatedException::ActionTerminatedException(string message) +: Exception(move(message)) { }