Modern C++ Kafka API
KafkaException.h
1 #pragma once
2 
3 #include <kafka/Project.h>
4 
5 #include <kafka/Error.h>
6 #include <kafka/RdKafkaHelper.h>
7 #include <kafka/Utility.h>
8 
9 #include <librdkafka/rdkafka.h>
10 
11 #include <chrono>
12 #include <exception>
13 #include <string>
14 
15 
16 namespace KAFKA_API {
17 
21 class KafkaException: public std::exception
22 {
23 public:
24  KafkaException(const char* filename, std::size_t lineno, const Error& error)
25  : _when(std::chrono::system_clock::now()),
26  _filename(filename),
27  _lineno(lineno),
28  _error(std::make_shared<Error>(error))
29  {}
30 
34  const Error& error() const { return *_error; }
35 
39  const char* what() const noexcept override
40  {
41  _what = utility::getLocalTimeString(_when) + ": " + _error->toString() + " (" + std::string(_filename) + ":" + std::to_string(_lineno) + ")";
42  return _what.c_str();
43  }
44 
45 private:
46  using TimePoint = std::chrono::system_clock::time_point;
47 
48  const TimePoint _when;
49  const std::string _filename;
50  const std::size_t _lineno;
51  const std::shared_ptr<Error> _error;
52  mutable std::string _what;
53 };
54 
55 
56 #define KAFKA_THROW_ERROR(error) throw KafkaException(__FILE__, __LINE__, error)
57 #define KAFKA_THROW_IF_WITH_ERROR(error) if (error) KAFKA_THROW_ERROR(error)
58 
59 } // end of KAFKA_API
60 
Unified error type.
Definition: Error.h:32
Specific exception for Kafka clients.
Definition: KafkaException.h:22
const Error & error() const
Obtains the underlying error.
Definition: KafkaException.h:34
const char * what() const noexcept override
Obtains explanatory string.
Definition: KafkaException.h:39