userver: userver/kafka/exceptions.hpp Source File
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/kafka/exceptions.hpp
4/// @brief Kafka client exceptions
5
6#include <cstdint>
7#include <exception>
8#include <map>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace kafka {
16
17/// @brief Base exception thrown by Producer::Send and Producer::SendAsync
18/// on send or delivery errors.
19class SendException : public std::runtime_error {
20public:
21 using std::runtime_error::runtime_error;
22
23 /// @brief Returns whether it makes sense to retry failed send.
24 ///
25 /// @see
26 /// https://docs.confluent.io/platform/current/clients/librdkafka/html/md_INTRODUCTION.html#autotoc_md8
27 bool IsRetryable() const noexcept;
28
29protected:
30 SendException(const char* what, bool is_retryable);
31
32private:
33 const bool is_retryable_{false};
34};
35
36/// @brief Base exception thrown by Producer::Send in bulk mode
37/// in case of one or more send errors.
38class BulkSendException : public std::runtime_error {
39 static constexpr const char* kWhat{"Some messages was not delivered."};
40
41public:
42 using ExceptionMap = std::map<std::size_t, std::exception_ptr>;
43
44 explicit BulkSendException(ExceptionMap nested_exceptions);
45
46 /// @return nested errors.
47 /// Nested exceptions are subclasses of SendException.
48 const ExceptionMap& GetExceptions() const noexcept;
49
50private:
51 /// @brief A mapping from the message's index in the bulk send operation
52 /// to the exception that occurred during its delivering.
53 /// @details Key: 0-based index of the element in the input batch.
54 /// Value: Pointer to the exception.
55 /// @note Contains only indices that resulted in an error.
56 const ExceptionMap nested_exceptions_;
57};
58
59class DeliveryTimeoutException final : public SendException {
60 static constexpr const char* kWhat{
61 "Message is not delivered after `delivery_timeout` milliseconds. Hint: "
62 "Adjust `delivery_timeout` and `queue_buffering_*` options or manually "
63 "retry the send request."
64 };
65
66public:
67 DeliveryTimeoutException();
68};
69
70class QueueFullException final : public SendException {
71 static constexpr const char* kWhat{
72 "The sending queue is full - send request cannot be scheduled. Hint: "
73 "Manually retry the error or increase `queue_buffering_max_messages` "
74 "and/or `queue_buffering_max_kbytes` config option."
75 };
76
77public:
78 QueueFullException();
79};
80
81class MessageTooLargeException final : public SendException {
82 static constexpr const char* kWhat{
83 "Message size exceeds configured limit. Hint: increase "
84 "`message_max_bytes` config option."
85 };
86
87public:
88 MessageTooLargeException();
89};
90
91class UnknownTopicException final : public SendException {
92 static constexpr const char* kWhat{"Given topic does not exist in cluster."};
93
94public:
95 UnknownTopicException();
96};
97
98class UnknownPartitionException final : public SendException {
99 static constexpr const char* kWhat = "Topic does not have given partition.";
100
101public:
102 UnknownPartitionException();
103};
104
105/// @brief Exception thrown when there is an error retrieving the offset range.
106class OffsetRangeException : public std::runtime_error {
107public:
108 using std::runtime_error::runtime_error;
109
110 OffsetRangeException(std::string_view what, std::string_view topic, std::uint32_t partition);
111};
112
113class OffsetRangeTimeoutException final : public OffsetRangeException {
114 static constexpr const char* kWhat = "Timeout while fetching offsets.";
115
116public:
117 OffsetRangeTimeoutException(std::string_view topic, std::uint32_t partition);
118};
119
120class TopicNotFoundException final : public std::runtime_error {
121public:
122 using std::runtime_error::runtime_error;
123};
124
125/// @brief Exception thrown when fetching metadata.
126class GetMetadataException : public std::runtime_error {
127public:
128 using std::runtime_error::runtime_error;
129
130 GetMetadataException(std::string_view what, std::string_view topic);
131};
132
133class GetMetadataTimeoutException final : public GetMetadataException {
134 static constexpr const char* kWhat = "Timeout while getting metadata.";
135
136public:
137 GetMetadataTimeoutException(std::string_view topic);
138};
139
140/// @brief Exception thrown when parsing consumed messages headers.
141/// @ref Message::GetHeaders
142class ParseHeadersException final : std::runtime_error {
143 static constexpr const char* kWhat = "Failed to parse headers";
144
145public:
146 ParseHeadersException(std::string_view error);
147};
148
149/// @brief Exception thrown when Seek* process failed.
150/// @ref ConsumerScope::Seek
151/// @ref ConsumerScope::SeekToBeginning
152/// @ref ConsumerScope::SeekToEnd
153class SeekException final : public std::runtime_error {
154public:
155 using std::runtime_error::runtime_error;
156};
157
158/// @brief Exception thrown when Seek* arguments are invalid.
159/// @ref ConsumerScope::Seek
160/// @ref ConsumerScope::SeekToBeginning
161/// @ref ConsumerScope::SeekToEnd
162class SeekInvalidArgumentException final : public std::invalid_argument {
163public:
164 using std::invalid_argument::invalid_argument;
165};
166
167/// @brief Fatal librdkafka consumer error; polling stops and consumer is recreated.
168/// @ref ConsumerImpl::ThrowIfRestartRequired
169class ConsumerRestartRequiredException final : public std::runtime_error {
170public:
171 explicit ConsumerRestartRequiredException(const std::string& message);
172};
173
174} // namespace kafka
175
176USERVER_NAMESPACE_END