zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
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 std::ostream& operator<<(std::ostream& os, const error_code& code)
16 {
17  switch (code)
18  {
19  case error_code::ok: return os << "ok";
20  default: return os << "error_code(" << static_cast<int>(code) << ')';
21  }
22 }
23 
24 std::string to_string(const error_code& code)
25 {
26  std::ostringstream os;
27  os << code;
28  return os.str();
29 }
30 
32 {
33  switch (code)
34  {
37  case error_code::not_implemented: throw not_implemented("unspecified");
41  case error_code::no_entry: throw no_entry();
46  case error_code::not_empty: throw not_empty();
49  case error_code::closed: throw closed();
54  default: throw error(code, "unknown");
55  }
56 }
57 
58 std::exception_ptr get_exception_ptr_of(error_code code)
59 {
60  try
61  {
62  throw_error(code);
63  }
64  catch (...)
65  {
66  return std::current_exception();
67  }
68 }
69 
71 // error_category //
73 
75 {
76 public:
77  virtual ptr<const char> name() const noexcept override { return "zookeeper"; }
78 
79  virtual std::string message(int condition) const override;
80 };
81 
82 std::string error_category_impl::message(int condition) const
83 {
84  return to_string(static_cast<error_code>(condition));
85 }
86 
88 {
89  static error_category_impl instance;
90  return instance;
91 }
92 
94 // errors //
96 
97 static std::string format_error(error_code code, const std::string& description)
98 {
99  if (description.empty())
100  return to_string(code);
101  else
102  return to_string(code) + ": " + description;
103 }
104 
105 error::error(error_code code, const std::string& description) :
106  std::system_error(static_cast<int>(code), error_category(), format_error(code, description)),
107  _code(code)
108 { }
109 
110 error::~error() noexcept = default;
111 
112 transport_error::transport_error(error_code code, const std::string& description) :
113  error(code, std::move(description))
114 { }
115 
116 transport_error::~transport_error() noexcept = default;
117 
118 connection_loss::connection_loss() :
119  transport_error(error_code::connection_loss, "")
120 { }
121 
122 connection_loss::~connection_loss() noexcept = default;
123 
124 marshalling_error::marshalling_error() :
125  transport_error(error_code::marshalling_error, "")
126 { }
127 
128 marshalling_error::~marshalling_error() noexcept = default;
129 
130 not_implemented::not_implemented(ptr<const char> op_name) :
131  error(error_code::not_implemented, std::string("Operation not implemented: ") + op_name)
132 { }
133 
134 not_implemented::~not_implemented() noexcept = default;
135 
136 invalid_arguments::invalid_arguments(error_code code, const std::string& description) :
137  error(code, description)
138 { }
139 
140 invalid_arguments::invalid_arguments() :
141  invalid_arguments(error_code::invalid_arguments, "")
142 { }
143 
144 invalid_arguments::~invalid_arguments() noexcept = default;
145 
148 { }
149 
150 authentication_failed::~authentication_failed() noexcept = default;
151 
152 invalid_ensemble_state::invalid_ensemble_state(error_code code, const std::string& description) :
153  error(code, description)
154 { }
155 
156 invalid_ensemble_state::~invalid_ensemble_state() noexcept = default;
157 
159  invalid_ensemble_state(error_code::new_configuration_no_quorum, "")
160 { }
161 
162 new_configuration_no_quorum::~new_configuration_no_quorum() noexcept = default;
163 
165  invalid_ensemble_state(error_code::reconfiguration_in_progress, "")
166 { }
167 
168 reconfiguration_in_progress::~reconfiguration_in_progress() noexcept = default;
169 
171  invalid_ensemble_state(error_code::reconfiguration_disabled, "")
172 { }
173 
174 reconfiguration_disabled::~reconfiguration_disabled() noexcept = default;
175 
176 invalid_connection_state::invalid_connection_state(error_code code, const std::string& description) :
177  error(code, description)
178 { }
179 
180 invalid_connection_state::~invalid_connection_state() noexcept = default;
181 
183  invalid_connection_state(error_code::session_expired, "")
184 { }
185 
186 session_expired::~session_expired() noexcept = default;
187 
189  invalid_connection_state(error_code::not_authorized, "")
190 { }
191 
192 not_authorized::~not_authorized() noexcept = default;
193 
194 closed::closed() :
195  invalid_connection_state(error_code::closed, "")
196 { }
197 
198 closed::~closed() noexcept = default;
199 
201  invalid_connection_state(error_code::ephemeral_on_local_session, "")
202 { }
203 
204 ephemeral_on_local_session::~ephemeral_on_local_session() noexcept = default;
205 
207  invalid_connection_state(error_code::read_only_connection, "")
208 { }
209 
210 read_only_connection::~read_only_connection() noexcept = default;
211 
212 check_failed::check_failed(error_code code, const std::string& description) :
213  error(code, description)
214 { }
215 
216 check_failed::~check_failed() noexcept = default;
217 
218 no_entry::no_entry() :
219  check_failed(error_code::no_entry, "")
220 { }
221 
222 no_entry::~no_entry() noexcept = default;
223 
225  check_failed(error_code::entry_exists, "")
226 { }
227 
228 entry_exists::~entry_exists() noexcept = default;
229 
230 not_empty::not_empty() :
231  check_failed(error_code::not_empty, "")
232 { }
233 
234 not_empty::~not_empty() noexcept = default;
235 
237  check_failed(error_code::version_mismatch, "")
238 { }
239 
240 version_mismatch::~version_mismatch() noexcept = default;
241 
243  check_failed(error_code::no_children_for_ephemerals, "")
244 { }
245 
246 no_children_for_ephemerals::~no_children_for_ephemerals() noexcept = default;
247 
248 transaction_failed::transaction_failed(error_code underlying_cause, std::size_t op_index) :
249  check_failed(error_code::transaction_failed,
250  std::string("Could not commit transaction due to ") + to_string(underlying_cause)
251  + " on operation " + std::to_string(op_index)
252  ),
253  _underlying_cause(underlying_cause),
254  _op_index(op_index)
255 { }
256 
257 transaction_failed::~transaction_failed() noexcept = default;
258 
259 }
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
Code for transaction_failed.
Code for new_configuration_no_quorum.
Code for connection_loss.
Operation was attempted that was not implemented.
Definition: error.hpp:167
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
Code for not_empty.
Code for no_children_for_ephemerals.
Never thrown.
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
Code for version_mismatch.
Invalid event (this should never be issued).
Code for entry_exists.
Code for no_entry.
Code for reconfiguration_disabled.
Code for not_implemented.
Code for ephemeral_on_local_session.
Code for reconfiguration_in_progress.
Code for invalid_arguments.
Code for read_only_connection.
Code for authentication_failed.
Code for closed.
Code for not_authorized.
Code for session_expired.
error_code
Code for all error types thrown by the client library.
Definition: error.hpp:18