CppWAMP
C++11 client library for the WAMP protocol
error.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_ERROR_HPP
8 #define CPPWAMP_ERROR_HPP
9 
10 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
14 
15 #include <sstream>
16 #include <stdexcept>
17 #include <string>
18 #include <system_error>
19 #include "api.hpp"
20 #include "config.hpp"
21 
22 //------------------------------------------------------------------------------
25 //------------------------------------------------------------------------------
26 #define CPPWAMP_LOGIC_ERROR(msg) \
27  error::Logic::raise(__FILE__, __LINE__, (msg));
28 
29 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 #define CPPWAMP_LOGIC_CHECK(cond, msg) \
37  {error::Logic::check((cond), __FILE__, __LINE__, (msg));}
38 
39 namespace wamp
40 {
41 
42 //******************************************************************************
43 // Exception Types
44 //******************************************************************************
45 
46 namespace error
47 {
48 
49 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 class CPPWAMP_API Failure : public std::system_error
53 {
54 public:
56  static std::string makeMessage(std::error_code ec);
57 
60  static std::string makeMessage(std::error_code ec, const std::string& info);
61 
63  explicit Failure(std::error_code ec);
64 
66  Failure(std::error_code ec, const std::string& info);
67 };
68 
69 
70 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 struct CPPWAMP_API Logic : public std::logic_error
74 {
75  using std::logic_error::logic_error;
76 
78  static void raise(const char* file, int line, const std::string& msg);
79 
82  static void check(bool condition, const char* file, int line,
83  const std::string& msg);
84 };
85 
86 //------------------------------------------------------------------------------
88 //------------------------------------------------------------------------------
89 struct CPPWAMP_API BadType : public std::runtime_error
90 {
91  explicit BadType(const std::string& what);
92 };
93 
94 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
97 struct CPPWAMP_API Access : public BadType
98 {
99  explicit Access(const std::string& what);
100  Access(const std::string& from, const std::string& to);
101 };
102 
103 //------------------------------------------------------------------------------
105 //------------------------------------------------------------------------------
106 struct CPPWAMP_API Conversion : public BadType
107 {
108  explicit Conversion(const std::string& what);
109 };
110 
111 //------------------------------------------------------------------------------
114 //------------------------------------------------------------------------------
115 struct CPPWAMP_API Decode: public std::runtime_error
116 {
117  explicit Decode(const std::string& what);
118 };
119 
120 } // namespace error
121 
122 
123 //******************************************************************************
124 // Session Error Codes
125 //******************************************************************************
126 
127 //------------------------------------------------------------------------------
141 //------------------------------------------------------------------------------
142 enum class SessionErrc
143 {
144  // Generic errors
145  success = 0,
146  sessionEnded,
149  joinError,
150  publishError,
153  registerError,
155  callError,
156  invalidState,
157 
158  // Errors mapped to predefined URIs
159  invalidUri,
166  closeRealm,
167  goodbyeAndOut,
168  notAuthorized,
170  noSuchRealm,
171  noSuchRole,
172  cancelled,
177 };
178 
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 class CPPWAMP_API SessionCategory : public std::error_category
184 {
185 public:
187  virtual const char* name() const noexcept override;
188 
190  virtual std::string message(int ev) const override;
191 
193  virtual bool equivalent(const std::error_code& code,
194  int condition) const noexcept override;
195 
196 private:
197  CPPWAMP_HIDDEN SessionCategory();
198 
199  friend SessionCategory& wampCategory();
200 };
201 
202 //------------------------------------------------------------------------------
205 //------------------------------------------------------------------------------
206 CPPWAMP_API SessionCategory& wampCategory();
207 
208 //------------------------------------------------------------------------------
211 //-----------------------------------------------------------------------------
212 CPPWAMP_API std::error_code make_error_code(SessionErrc errc);
213 
214 //------------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------
218 CPPWAMP_API std::error_condition make_error_condition(SessionErrc errc);
219 
220 //------------------------------------------------------------------------------
223 //-----------------------------------------------------------------------------
224 CPPWAMP_API bool lookupWampErrorUri(const std::string& uri,
225  SessionErrc fallback, SessionErrc& result);
226 
227 
228 //******************************************************************************
229 // Codec decoding Error Codes
230 //******************************************************************************
231 
232 //------------------------------------------------------------------------------
240 //------------------------------------------------------------------------------
241 enum class DecodingErrc
242 {
243  success = 0,
244  failure = 1,
245  emptyInput = 2,
246  expectedStringKey = 3,
247  badBase64Length = 4,
248  badBase64Padding = 5,
249  badBase64Char = 6
250 };
251 
252 //------------------------------------------------------------------------------
255 //------------------------------------------------------------------------------
256 class CPPWAMP_API DecodingCategory : public std::error_category
257 {
258 public:
260  virtual const char* name() const noexcept override;
261 
263  virtual std::string message(int ev) const override;
264 
266  virtual bool equivalent(const std::error_code& code,
267  int condition) const noexcept override;
268 
269 private:
270  CPPWAMP_HIDDEN DecodingCategory();
271 
272  friend DecodingCategory& decodingCategory();
273 };
274 
275 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
280 CPPWAMP_API DecodingCategory& decodingCategory();
281 
282 //------------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
286 CPPWAMP_API std::error_code make_error_code(DecodingErrc errc);
287 
288 //------------------------------------------------------------------------------
291 //-----------------------------------------------------------------------------
292 CPPWAMP_API std::error_condition make_error_condition(DecodingErrc errc);
293 
294 
295 //******************************************************************************
296 // Protocol Error Codes
297 //******************************************************************************
298 
299 //------------------------------------------------------------------------------
307 //------------------------------------------------------------------------------
308 enum class ProtocolErrc
309 {
310  success = 0,
311  badDecode,
312  badSchema,
315 };
316 
317 //------------------------------------------------------------------------------
321 //------------------------------------------------------------------------------
322 class CPPWAMP_API ProtocolCategory : public std::error_category
323 {
324 public:
326  virtual const char* name() const noexcept override;
327 
329  virtual std::string message(int ev) const override;
330 
332  virtual bool equivalent(const std::error_code& code,
333  int condition) const noexcept override;
334 
335 private:
336  CPPWAMP_HIDDEN ProtocolCategory();
337 
338  friend ProtocolCategory& protocolCategory();
339 };
340 
341 //------------------------------------------------------------------------------
344 //------------------------------------------------------------------------------
345 CPPWAMP_API ProtocolCategory& protocolCategory();
346 
347 //------------------------------------------------------------------------------
350 //-----------------------------------------------------------------------------
351 CPPWAMP_API std::error_code make_error_code(ProtocolErrc errc);
352 
353 //------------------------------------------------------------------------------
356 //-----------------------------------------------------------------------------
357 CPPWAMP_API std::error_condition make_error_condition(ProtocolErrc errc);
358 
359 
360 //******************************************************************************
361 // Transport Error Codes
362 //******************************************************************************
363 
364 //------------------------------------------------------------------------------
366 //------------------------------------------------------------------------------
367 enum class TransportErrc
368 {
369  success = 0,
370  aborted = 1,
371  failed = 2,
372  badTxLength = 3,
373  badRxLength = 4
374 };
375 
376 //------------------------------------------------------------------------------
379 //------------------------------------------------------------------------------
380 class CPPWAMP_API TransportCategory : public std::error_category
381 {
382 public:
384  virtual const char* name() const noexcept override;
385 
387  virtual std::string message(int ev) const override;
388 
390  virtual bool equivalent(const std::error_code& code,
391  int condition) const noexcept override;
392 
393 private:
394  CPPWAMP_HIDDEN TransportCategory();
395 
396  friend TransportCategory& transportCategory();
397 };
398 
399 //------------------------------------------------------------------------------
403 //------------------------------------------------------------------------------
404 CPPWAMP_API TransportCategory& transportCategory();
405 
406 //------------------------------------------------------------------------------
409 //-----------------------------------------------------------------------------
410 CPPWAMP_API std::error_code make_error_code(TransportErrc errc);
411 
412 //------------------------------------------------------------------------------
415 //-----------------------------------------------------------------------------
416 CPPWAMP_API std::error_condition make_error_condition(TransportErrc errc);
417 
418 
419 //******************************************************************************
420 // Raw Socket Error Codes
421 //******************************************************************************
422 
423 //------------------------------------------------------------------------------
425 //------------------------------------------------------------------------------
426 enum class RawsockErrc
427 {
428  success = 0,
429  badSerializer = 1,
430  badMaxLength = 2,
431  reservedBitsUsed = 3,
433  // 5-15 reserved for future WAMP raw socket error responses
434 
435  badHandshake = 16,
436  badMessageType = 17
437 };
438 
439 //------------------------------------------------------------------------------
443 //------------------------------------------------------------------------------
444 class CPPWAMP_API RawsockCategory : public std::error_category
445 {
446 public:
448  virtual const char* name() const noexcept override;
449 
451  virtual std::string message(int ev) const override;
452 
454  virtual bool equivalent(const std::error_code& code,
455  int condition) const noexcept override;
456 
457 private:
458  CPPWAMP_HIDDEN RawsockCategory();
459 
460  friend RawsockCategory& rawsockCategory();
461 };
462 
463 //------------------------------------------------------------------------------
467 //------------------------------------------------------------------------------
468 CPPWAMP_API RawsockCategory& rawsockCategory();
469 
470 //------------------------------------------------------------------------------
473 //-----------------------------------------------------------------------------
474 CPPWAMP_API std::error_code make_error_code(RawsockErrc errc);
475 
476 //------------------------------------------------------------------------------
479 //-----------------------------------------------------------------------------
480 CPPWAMP_API std::error_condition make_error_condition(RawsockErrc errc);
481 
482 } // namespace wamp
483 
484 
485 //------------------------------------------------------------------------------
486 #if !defined CPPWAMP_FOR_DOXYGEN
487 namespace std
488 {
489 
490 template <>
491 struct CPPWAMP_API is_error_condition_enum<wamp::SessionErrc>
492  : public true_type
493 {};
494 
495 template <>
496 struct CPPWAMP_API is_error_condition_enum<wamp::DecodingErrc>
497  : public true_type
498 {};
499 
500 template <>
501 struct CPPWAMP_API is_error_condition_enum<wamp::ProtocolErrc>
502  : public true_type
503 {};
504 
505 template <>
506 struct CPPWAMP_API is_error_condition_enum<wamp::TransportErrc>
507  : public true_type
508 {};
509 
510 template <>
511 struct CPPWAMP_API is_error_condition_enum<wamp::RawsockErrc>
512  : public true_type
513 {};
514 
515 } // namespace std
516 #endif // !defined CPPWAMP_FOR_DOXYGEN
517 
518 
519 #ifndef CPPWAMP_COMPILED_LIB
520 #include "internal/error.ipp"
521 #endif
522 
523 #endif // CPPWAMP_ERROR_HPP
wamp::SessionErrc::authorizationFailed
@ authorizationFailed
The authorization operation failed.
wamp::error::BadType
Base class for exceptions involving invalid Variant types.
Definition: error.hpp:89
wamp::TransportErrc::failed
@ failed
Operation failed.
wamp::ProtocolErrc::unexpectedMsg
@ unexpectedMsg
Received unexpected WAMP message.
wamp::RawsockCategory
std::error_category used for reporting errors specific to raw socket transports.
Definition: error.hpp:444
wamp::TransportErrc::badRxLength
@ badRxLength
Incoming message exceeds maximum length.
wamp::DecodingErrc
DecodingErrc
Error code values used with the DecodingCategory error category.
Definition: error.hpp:241
wamp::DecodingCategory
std::error_category used for reporting deserialization errors.
Definition: error.hpp:256
wamp::lookupWampErrorUri
bool lookupWampErrorUri(const std::string &uri, SessionErrc fallback, SessionErrc &result)
Definition: error.ipp:238
wamp::SessionErrc::optionNotAllowed
@ optionNotAllowed
Option is disallowed by the router.
wamp::SessionCategory
std::error_category used for reporting errors at the WAMP session layer.
Definition: error.hpp:183
wamp::SessionErrc::noSuchProcedure
@ noSuchProcedure
No procedure was registered under the given URI.
wamp::SessionErrc::networkFailure
@ networkFailure
Router encountered a network failure.
wamp::SessionErrc::registerError
@ registerError
Register error reported by dealer.
wamp::SessionErrc::procedureAlreadyExists
@ procedureAlreadyExists
A procedure with the given URI is already registered.
wamp::DecodingErrc::success
@ success
Operation succesful.
wamp::TransportErrc::badTxLength
@ badTxLength
Outgoing message exceeds maximum length.
wamp::RawsockErrc::success
@ success
Operation succesful.
wamp::TransportCategory
std::error_category used for reporting errors at the transport layer.
Definition: error.hpp:380
wamp::ProtocolErrc::unsupportedMsg
@ unsupportedMsg
Received unsupported WAMP message.
api.hpp
Defines macros related to exporting/importing APIs.
wamp::DecodingErrc::badBase64Char
@ badBase64Char
Invalid Base64 character.
wamp::ProtocolErrc
ProtocolErrc
Error code values used with the ProtocolCategory error category.
Definition: error.hpp:308
wamp::DecodingErrc::badBase64Length
@ badBase64Length
Invalid Base64 string length.
wamp::SessionErrc::cancelled
@ cancelled
A previously issued call was cancelled.
wamp::DecodingErrc::failure
@ failure
Decoding failed.
wamp::error::Failure
General purpose runtime exception that wraps a std::error_code.
Definition: error.hpp:52
wamp::SessionErrc::joinError
@ joinError
Join error reported by router.
wamp::TransportErrc::success
@ success
Operation successful.
wamp::SessionErrc::callError
@ callError
Call error reported by callee or dealer.
wamp::DecodingErrc::badBase64Padding
@ badBase64Padding
Invalid Base64 padding.
wamp::SessionErrc::discloseMeDisallowed
@ discloseMeDisallowed
Router rejected client request to disclose its identity.
wamp::SessionErrc::notAuthorized
@ notAuthorized
This peer is not authorized to perform the operation.
wamp::SessionErrc::allTransportsFailed
@ allTransportsFailed
All transports failed during connection.
wamp::SessionErrc::noSuchRegistration
@ noSuchRegistration
Could not unregister; the given registration is not active.
wamp::RawsockErrc::reservedBitsUsed
@ reservedBitsUsed
Use of reserved bits (unsupported feature)
wamp::SessionErrc::goodbyeAndOut
@ goodbyeAndOut
Session ended successfully.
wamp::ProtocolErrc::badSchema
@ badSchema
Invalid WAMP message schema.
wamp::SessionErrc::sessionEndedByPeer
@ sessionEndedByPeer
Session ended by other peer.
wamp
Definition: anyhandler.hpp:36
wamp::ProtocolErrc::success
@ success
Operation successful.
wamp::RawsockErrc
RawsockErrc
Error code values used with the RawsockCategory error category.
Definition: error.hpp:426
wamp::RawsockErrc::badHandshake
@ badHandshake
Invalid handshake format from peer.
wamp::SessionErrc::invalidArgument
@ invalidArgument
The given argument types/values are not acceptable to the called procedure.
wamp::SessionErrc::publishError
@ publishError
Publish error reported by broker.
wamp::SessionErrc::success
@ success
Operation successful.
wamp::SessionErrc::invalidUri
@ invalidUri
An invalid WAMP URI was provided.
wamp::SessionErrc::sessionEnded
@ sessionEnded
Operation aborted; session ended by this peer.
wamp::SessionErrc::closeRealm
@ closeRealm
The other peer is leaving the realm.
wamp::SessionErrc::invalidState
@ invalidState
Invalid state for this operation.
wamp::RawsockErrc::badSerializer
@ badSerializer
Serializer unsupported.
wamp::SessionErrc::unsubscribeError
@ unsubscribeError
Unsubscribe error reported by broker.
wamp::error::Decode
Exception type thrown when codec deserialization fails.
Definition: error.hpp:115
wamp::RawsockErrc::badMessageType
@ badMessageType
Invalid message type.
wamp::SessionErrc::noSuchRealm
@ noSuchRealm
Attempt to join non-existent realm.
wamp::DecodingErrc::emptyInput
@ emptyInput
Input is empty or has no tokens.
wamp::TransportErrc::aborted
@ aborted
Operation aborted.
wamp::SessionErrc::noSuchSubscription
@ noSuchSubscription
Could not unsubscribe; the given subscription is not active.
wamp::error::Logic
Exception thrown when a pre-condition is not met.
Definition: error.hpp:73
wamp::SessionErrc::noSuchRole
@ noSuchRole
Attempt to authenticate under unsupported role.
wamp::SessionErrc::systemShutdown
@ systemShutdown
The other peer is shutting down.
wamp::error::Conversion
Exception type thrown when converting a Variant to an invalid type.
Definition: error.hpp:106
wamp::RawsockErrc::badMaxLength
@ badMaxLength
Maximum message length unacceptable.
wamp::DecodingErrc::expectedStringKey
@ expectedStringKey
Expected a string key.
wamp::SessionErrc::subscribeError
@ subscribeError
Subscribe error reported by broker.
wamp::ProtocolErrc::badDecode
@ badDecode
Error decoding WAMP message payload.
wamp::RawsockErrc::maxConnectionsReached
@ maxConnectionsReached
Maximum connection count reached.
wamp::SessionErrc
SessionErrc
Error code values used with the SessionCategory error category.
Definition: error.hpp:142
wamp::ProtocolCategory
std::error_category used for reporting protocol errors related to invalid WAMP messages.
Definition: error.hpp:322
wamp::TransportErrc
TransportErrc
Error code values used with the TransportCategory error category.
Definition: error.hpp:367
wamp::error::Access
Exception type thrown when accessing a Variant as an invalid type.
Definition: error.hpp:97
wamp::SessionErrc::noEligibleCallee
@ noEligibleCallee
Call options lead to the exclusion of all callees providing the procedure.
wamp::SessionErrc::unregisterError
@ unregisterError
Unregister error reported by dealer.