zookeeper-cpp
ZooKeeper Client for C++
error.cpp
1 #include "error.hpp"
2 
3 #include <sstream>
4 #include <ostream>
5 
6 #include <zookeeper/zookeeper.h>
7 
8 namespace zk
9 {
10 
12 // error_code //
14 
15 const std::array<error_code, ZKPP_ERROR_CODE_LIST(ZKPP_ERROR_CODE_SIZE_IMPL)>& all_error_codes()
16 {
17  #define ZKPP_ERROR_CODE_VALUE_IMPL(cxx_name, number, c_name) error_code::cxx_name,
18  static std::array<error_code, ZKPP_ERROR_CODE_LIST(ZKPP_ERROR_CODE_SIZE_IMPL)> instance =
19  {
20  ZKPP_ERROR_CODE_LIST(ZKPP_ERROR_CODE_VALUE_IMPL)
21  };
22  return instance;
23 }
24 
25 std::ostream& operator<<(std::ostream& os, const error_code& code)
26 {
27  switch (code)
28  {
29  #define ZKPP_ERROR_CODE_OSTREAM_CASE_IMPL(cxx_name, number, c_name) \
30  case error_code::cxx_name: return os << #cxx_name;
31  ZKPP_ERROR_CODE_LIST(ZKPP_ERROR_CODE_OSTREAM_CASE_IMPL)
32  default:
33  return os << "error_code(" << static_cast<int>(code) << ')';
34  }
35 }
36 
37 std::string to_string(const error_code& code)
38 {
39  std::ostringstream os;
40  os << code;
41  return os.str();
42 }
43 
44 void throw_error(error_code code)
45 {
46  switch (code)
47  {
48  case error_code::runtime_inconsistency: throw runtime_inconsistency();
49  case error_code::data_inconsistency: throw data_inconsistency();
50  case error_code::connection_loss: throw connection_loss();
51  case error_code::marshalling_error: throw marshalling_error();
52  case error_code::unimplemented: throw unimplemented();
53  case error_code::operation_timeout: throw operation_timeout();
54  case error_code::invalid_arguments: throw invalid_arguments();
55  case error_code::invalid_handle_state: throw invalid_handle_state();
56  case error_code::unknown_session: throw unknown_session();
57  case error_code::new_configuration_no_quorum: throw new_configuration_no_quorum();
58  case error_code::reconfiguration_in_progress: throw reconfiguration_in_progress();
59  case error_code::no_node: throw no_node();
60  case error_code::not_authenticated: throw not_authenticated();
61  case error_code::bad_version: throw bad_version();
62  case error_code::no_children_for_ephemerals: throw no_children_for_ephemerals();
63  case error_code::node_exists: throw node_exists();
64  case error_code::not_empty: throw not_empty();
65  case error_code::session_expired: throw session_expired();
66  case error_code::invalid_callback: throw invalid_callback();
67  case error_code::invalid_acl: throw invalid_acl();
69  case error_code::closing: throw closing();
70  case error_code::no_response: throw no_response();
71  case error_code::session_moved: throw session_moved();
72  case error_code::server_read_only: throw server_read_only();
73  case error_code::ephemeral_on_local_session: throw ephemeral_on_local_session();
74  case error_code::no_watcher: throw no_watcher();
75  case error_code::reconfiguration_disabled: throw reconfiguration_disabled();
76  case error_code::transaction_failed: throw transaction_failed(code, 0);
77  default: throw unknown_error(code);
78  }
79 }
80 
81 std::exception_ptr get_exception_ptr_of(error_code code)
82 {
83  try
84  {
85  throw_error(code);
86  }
87  catch (...)
88  {
89  return std::current_exception();
90  }
91 }
92 
94 // errors //
96 
97 error::error(error_code code, std::string description) :
98  std::runtime_error(to_string(code) + ": " + description),
99  _code(code)
100 { }
101 
102 error::~error() noexcept = default;
103 
104 system_error::system_error(error_code code, std::string description) :
105  error(code, std::move(description))
106 { }
107 
108 system_error::~system_error() noexcept = default;
109 
110 runtime_inconsistency::runtime_inconsistency() :
111  system_error(error_code::runtime_inconsistency, "A runtime inconsistency was found")
112 { }
113 
114 runtime_inconsistency::~runtime_inconsistency() noexcept = default;
115 
116 data_inconsistency::data_inconsistency() :
117  system_error(error_code::data_inconsistency, "A data inconsistency was found")
118 { }
119 
120 data_inconsistency::~data_inconsistency() noexcept = default;
121 
122 connection_loss::connection_loss() :
123  system_error(error_code::connection_loss, "Connection to the server has been lost")
124 { }
125 
126 connection_loss::~connection_loss() noexcept = default;
127 
128 marshalling_error::marshalling_error() :
129  system_error(error_code::marshalling_error, "Error while marshalling or unmarshalling data")
130 { }
131 
132 marshalling_error::~marshalling_error() noexcept = default;
133 
134 unimplemented::unimplemented() :
135  system_error(error_code::unimplemented, "Operation is unimplemented")
136 { }
137 
138 unimplemented::~unimplemented() noexcept = default;
139 
140 operation_timeout::operation_timeout() :
141  system_error(error_code::operation_timeout, "Operation timeout")
142 { }
143 
144 operation_timeout::~operation_timeout() noexcept = default;
145 
146 invalid_arguments::invalid_arguments() :
147  system_error(error_code::invalid_arguments, "Invalid arguments")
148 { }
149 
150 invalid_arguments::~invalid_arguments() noexcept = default;
151 
152 invalid_handle_state::invalid_handle_state() :
153  system_error(error_code::invalid_handle_state, "Invalid state for handle")
154 { }
155 
156 invalid_handle_state::~invalid_handle_state() noexcept = default;
157 
158 unknown_session::unknown_session() :
159  system_error(error_code::unknown_session, "Unknown session")
160 { }
161 
162 unknown_session::~unknown_session() noexcept = default;
163 
164 new_configuration_no_quorum::new_configuration_no_quorum() :
165  system_error(error_code::new_configuration_no_quorum,
166  "No quorum of new configuration is connected and up-to-date with the leader of last commmitted "
167  "configuration. Try invoking reconfiguration after new servers are connected and synced."
168  )
169 { }
170 
171 new_configuration_no_quorum::~new_configuration_no_quorum() noexcept = default;
172 
173 reconfiguration_in_progress::reconfiguration_in_progress() :
174  system_error(error_code::reconfiguration_in_progress,
175  "Another reconfiguration is in progress -- concurrent reconfigs not supported"
176  )
177 { }
178 
179 reconfiguration_in_progress::~reconfiguration_in_progress() noexcept = default;
180 
181 api_error::api_error(error_code code, std::string description) :
182  error(code, std::move(description))
183 { }
184 
185 api_error::~api_error() noexcept = default;
186 
187 no_node::no_node() :
188  api_error(error_code::no_node, "Node does not exist")
189 { }
190 
191 no_node::~no_node() noexcept = default;
192 
193 not_authenticated::not_authenticated() :
194  api_error(error_code::not_authenticated, "Not authenticated")
195 { }
196 
197 not_authenticated::~not_authenticated() noexcept = default;
198 
199 bad_version::bad_version() :
200  api_error(error_code::bad_version, "Version conflict")
201 { }
202 
203 bad_version::~bad_version() noexcept = default;
204 
205 no_children_for_ephemerals::no_children_for_ephemerals() :
206  api_error(error_code::no_children_for_ephemerals, "Ephemeral nodes may not have children")
207 { }
208 
209 no_children_for_ephemerals::~no_children_for_ephemerals() noexcept = default;
210 
211 node_exists::node_exists() :
212  api_error(error_code::node_exists, "Node already exists")
213 { }
214 
215 node_exists::~node_exists() noexcept = default;
216 
217 not_empty::not_empty() :
218  api_error(error_code::not_empty, "Cannot erase a node with children")
219 { }
220 
221 not_empty::~not_empty() noexcept = default;
222 
223 session_expired::session_expired() :
224  api_error(error_code::session_expired, "The session has been expired by the server")
225 { }
226 
227 session_expired::~session_expired() noexcept = default;
228 
229 invalid_callback::invalid_callback() :
230  api_error(error_code::invalid_callback, "Invalid callback specified")
231 { }
232 
233 invalid_callback::~invalid_callback() noexcept = default;
234 
235 invalid_acl::invalid_acl() :
236  api_error(error_code::invalid_acl, "Invalid ACL specified")
237 { }
238 
239 invalid_acl::~invalid_acl() noexcept = default;
240 
242  api_error(error_code::authentication_failed, "Client authentication failed")
243 { }
244 
245 authentication_failed::~authentication_failed() noexcept = default;
246 
247 closing::closing() :
248  api_error(error_code::closing, "Client is closing")
249 { }
250 
251 closing::~closing() noexcept = default;
252 
253 no_response::no_response() :
254  api_error(error_code::no_response, "No server responses to process")
255 { }
256 
257 no_response::~no_response() noexcept = default;
258 
259 session_moved::session_moved() :
260  api_error(error_code::session_moved, "Session moved to another server, so operation was ignored")
261 { }
262 
263 session_moved::~session_moved() noexcept = default;
264 
265 server_read_only::server_read_only() :
266  api_error(error_code::server_read_only, "State-changing request was passed to read-only server")
267 { }
268 
269 server_read_only::~server_read_only() noexcept = default;
270 
271 ephemeral_on_local_session::ephemeral_on_local_session() :
272  api_error(error_code::ephemeral_on_local_session, "Cannot create ephemeral node on a local session")
273 { }
274 
275 ephemeral_on_local_session::~ephemeral_on_local_session() noexcept = default;
276 
277 no_watcher::no_watcher() :
278  api_error(error_code::no_watcher, "Cannot remove a non-existing watcher")
279 { }
280 
281 no_watcher::~no_watcher() noexcept = default;
282 
283 reconfiguration_disabled::reconfiguration_disabled() :
284  api_error(error_code::reconfiguration_disabled, "Cluster reconfiguration feature is disabled")
285 { }
286 
287 reconfiguration_disabled::~reconfiguration_disabled() noexcept = default;
288 
289 transaction_failed::transaction_failed(error_code underlying_cause, std::size_t op_index) :
290  api_error(error_code::transaction_failed,
291  std::string("Could not commit transaction due to ") + to_string(underlying_cause) + " on operation "
292  + std::to_string(op_index)
293  ),
294  _underlying_cause(underlying_cause),
295  _op_index(op_index)
296 { }
297 
298 transaction_failed::~transaction_failed() noexcept = default;
299 
300 unknown_error::unknown_error(error_code code) :
301  error(code, "Error code not recognized")
302 { }
303 
304 unknown_error::~unknown_error() noexcept = default;
305 
306 }
This value is issued as part of an event when the state changes.
Definition: acl.cpp:8
STL namespace.
Authentication has failed – connection requires a new connection instance with different credentials...
The client is in the connected state – it is connected to a server in the ensemble (one of the serve...
The znode will be deleted upon the client&#39;s disconnect.
Invalid event (this should never be issued).
You can access the data of a node and can list its children.