zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
error.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 
5 #include <iosfwd>
6 #include <string>
7 #include <system_error>
8 
9 namespace zk
10 {
11 
14 
18 enum class error_code : int
19 {
20  ok = 0,
21  connection_loss = -4,
22  marshalling_error = -5,
23  not_implemented = -6,
24  invalid_arguments = -8,
27  no_entry = -101,
28  not_authorized = -102,
29  version_mismatch = -103,
31  entry_exists = -110,
32  not_empty = -111,
33  session_expired = -112,
34  authentication_failed = -115,
35  closed = -116,
36  read_only_connection = -119,
39  transaction_failed = -199,
40 };
41 
43 inline constexpr bool is_transport_error(error_code code)
44 {
45  return code == error_code::connection_loss
47 }
48 
50 inline constexpr bool is_invalid_arguments(error_code code)
51 {
52  return code == error_code::invalid_arguments
54 }
55 
57 inline constexpr bool is_invalid_ensemble_state(error_code code)
58 {
62 }
63 
65 inline constexpr bool is_invalid_connection_state(error_code code)
66 {
67  return code == error_code::closed
71  || code == error_code::session_expired;
72 }
73 
75 inline constexpr bool is_check_failed(error_code code)
76 {
78  || code == error_code::no_entry
79  || code == error_code::entry_exists
80  || code == error_code::not_empty
83 }
84 
85 std::ostream& operator<<(std::ostream&, const error_code&);
86 
87 std::string to_string(const error_code&);
88 
91 [[noreturn]]
92 void throw_error(error_code code);
93 
97 std::exception_ptr get_exception_ptr_of(error_code code);
98 
103 
107 class error :
108  public std::system_error
109 {
110 public:
111  explicit error(error_code code, const std::string& description);
112 
113  virtual ~error() noexcept;
114 
116  error_code code() const { return _code; }
117 
118 private:
119  error_code _code;
120 };
121 
126  public error
127 {
128 public:
129  explicit transport_error(error_code code, const std::string& description);
130 
131  virtual ~transport_error() noexcept;
132 };
133 
141 class connection_loss final :
142  public transport_error
143 {
144 public:
145  explicit connection_loss();
146 
147  virtual ~connection_loss() noexcept;
148 };
149 
156 class marshalling_error final :
157  public transport_error
158 {
159 public:
160  explicit marshalling_error();
161 
162  virtual ~marshalling_error() noexcept;
163 };
164 
167 class not_implemented final :
168  public error
169 {
170 public:
172  explicit not_implemented(ptr<const char> op_name);
173 
174  virtual ~not_implemented() noexcept;
175 };
176 
179  public error
180 {
181 public:
182  explicit invalid_arguments(error_code code, const std::string& description);
183 
184  explicit invalid_arguments();
185 
186  virtual ~invalid_arguments() noexcept;
187 };
188 
196  public invalid_arguments
197 {
198 public:
199  explicit authentication_failed();
200 
201  virtual ~authentication_failed() noexcept;
202 };
203 
208  public error
209 {
210 public:
211  explicit invalid_ensemble_state(error_code code, const std::string& description);
212 
213  virtual ~invalid_ensemble_state() noexcept;
214 };
215 
221 {
222 public:
223  explicit new_configuration_no_quorum();
224 
225  virtual ~new_configuration_no_quorum() noexcept;
226 };
227 
232 {
233 public:
234  explicit reconfiguration_in_progress();
235 
236  virtual ~reconfiguration_in_progress() noexcept;
237 };
238 
242 {
243 public:
244  explicit reconfiguration_disabled();
245 
246  virtual ~reconfiguration_disabled() noexcept;
247 };
248 
251  public error
252 {
253 public:
254  explicit invalid_connection_state(error_code code, const std::string& description);
255 
256  virtual ~invalid_connection_state() noexcept;
257 };
258 
270 class session_expired final :
272 {
273 public:
274  explicit session_expired();
275 
276  virtual ~session_expired() noexcept;
277 };
278 
280 class not_authorized final :
282 {
283 public:
284  explicit not_authorized();
285 
286  virtual ~not_authorized() noexcept;
287 };
288 
291 class closed final :
293 {
294 public:
295  explicit closed();
296 
297  virtual ~closed() noexcept;
298 };
299 
305 {
306 public:
307  explicit ephemeral_on_local_session();
308 
309  virtual ~ephemeral_on_local_session() noexcept;
310 };
311 
315 class read_only_connection final :
317 {
318 public:
319  explicit read_only_connection();
320 
321  virtual ~read_only_connection() noexcept;
322 };
323 
327  public error
328 {
329 public:
330  explicit check_failed(error_code code, const std::string& description);
331 
332  virtual ~check_failed() noexcept;
333 };
334 
336 class no_entry final :
337  public check_failed
338 {
339 public:
340  explicit no_entry();
341 
342  virtual ~no_entry() noexcept;
343 };
344 
346 class entry_exists final :
347  public check_failed
348 {
349 public:
350  explicit entry_exists();
351 
352  virtual ~entry_exists() noexcept;
353 };
354 
356 class not_empty final :
357  public check_failed
358 {
359 public:
360  explicit not_empty();
361 
362  virtual ~not_empty() noexcept;
363 };
364 
367 class version_mismatch final :
368  public check_failed
369 {
370 public:
371  explicit version_mismatch();
372 
373  virtual ~version_mismatch() noexcept;
374 };
375 
378  public check_failed
379 {
380 public:
381  explicit no_children_for_ephemerals();
382 
383  virtual ~no_children_for_ephemerals() noexcept;
384 };
385 
388 class transaction_failed final :
389  public check_failed
390 {
391 public:
392  explicit transaction_failed(error_code code, std::size_t op_index);
393 
394  virtual ~transaction_failed() noexcept;
395 
398  error_code underlying_cause() const { return _underlying_cause; }
399 
402  std::size_t failed_op_index() const { return _op_index; }
403 
404 private:
405  error_code _underlying_cause;
406  std::size_t _op_index;
407 };
408 
410 
411 }
412 
413 namespace std
414 {
415 
416 template <>
417 struct is_error_code_enum<zk::error_code> :
418  true_type
419 {
420 };
421 
422 }
T * ptr
A simple, unowned pointer.
Definition: config.hpp:37
const std::error_category & error_category()
Get the std::error_category capable of describing ZooKeeper-provided error codes. ...
Definition: error.cpp:87
An attempt was made to read or write to a ZNode when the connection does not have permission to do...
Definition: error.hpp:280
Code for transaction_failed.
constexpr bool is_invalid_connection_state(error_code code)
Check if the provided code is an exception code for a invalid_connection_state type of exception...
Definition: error.hpp:65
Code for new_configuration_no_quorum.
constexpr bool is_check_failed(error_code code)
Check if the provided code is an exception code for a check_failed type of exception.
Definition: error.hpp:75
Base exception for cases where the ensemble is in an invalid state to perform a given action...
Definition: error.hpp:207
Code for connection_loss.
Raised when attempting an ensemble reconfiguration, but the proposed new ensemble would not be able t...
Definition: error.hpp:219
Operation was attempted that was not implemented.
Definition: error.hpp:167
The ensemble does not support reconfiguration.
Definition: error.hpp:240
Thrown from read operations when attempting to read a ZNode that does not exist.
Definition: error.hpp:336
constexpr bool is_transport_error(error_code code)
Check if the provided code is an exception code for a transport_error type of exception.
Definition: error.hpp:43
constexpr bool is_invalid_ensemble_state(error_code code)
Check if the provided code is an exception code for a invalid_ensemble_state type of exception...
Definition: error.hpp:57
std::size_t failed_op_index() const
The transaction index which caused the error (0 indexed).
Definition: error.hpp:402
The connection is closed.
Definition: error.hpp:291
error_code code() const
The code representation of this error.
Definition: error.hpp:116
std::exception_ptr get_exception_ptr_of(error_code code)
Get an std::exception_ptr containing an exception with the proper type for the given code...
Definition: error.cpp:58
Base error type for all errors raised by this library.
Definition: error.hpp:107
Connection to the server has been lost before the attempted operation was verified as completed...
Definition: error.hpp:141
Code for not_empty.
Code for no_children_for_ephemerals.
error_code underlying_cause() const
The underlying cause that caused this transaction to be aborted.
Definition: error.hpp:398
Base exception for cases where a write operation was rolled back due to a failed check.
Definition: error.hpp:326
Thrown from client::commit when a transaction cannot be committed to the system.
Definition: error.hpp:388
Never thrown.
constexpr bool is_invalid_arguments(error_code code)
Check if the provided code is an exception code for a invalid_arguments type of exception.
Definition: error.hpp:50
Arguments to an operation were invalid.
Definition: error.hpp:178
Code for marshalling_error.
void throw_error(error_code code)
Throw an exception for the given code.
Definition: error.cpp:31
The server rejected the connection due to invalid authentication information.
Definition: error.hpp:195
An error occurred while marshalling data.
Definition: error.hpp:156
Base type for errors generated because the connection is misconfigured.
Definition: error.hpp:250
Code for version_mismatch.
Thrown from modification operations when a version check is specified and the value in the database d...
Definition: error.hpp:367
Code for entry_exists.
Base types for errors that occurred while transporting data across a network.
Definition: error.hpp:125
Code for no_entry.
Code for reconfiguration_disabled.
Ephemeral ZNodes cannot have children.
Definition: error.hpp:377
Code for ephemeral_on_local_session.
Thrown when attempting to create a ZNode, but one already exists at the specified path...
Definition: error.hpp:346
Code for reconfiguration_in_progress.
Code for invalid_arguments.
An attempt was made to create an ephemeral entry, but the connection has a local session.
Definition: error.hpp:303
not_implemented(ptr< const char > op_name)
Definition: error.cpp:130
Thrown when attempting to erase a ZNode that has children.
Definition: error.hpp:356
Code for read_only_connection.
Code for authentication_failed.
Code for closed.
The client session has been ended by the server.
Definition: error.hpp:270
A write operation was attempted on a read-only connection.
Definition: error.hpp:315
Code for not_authorized.
An attempt was made to reconfigure the ensemble, but there is already a reconfiguration in progress...
Definition: error.hpp:230
Code for session_expired.
error_code
Code for all error types thrown by the client library.
Definition: error.hpp:18