rad  3.0.0
errors.hpp
Go to the documentation of this file.
1 
10 #ifndef RAD_ERRORS_HPP
11 #define RAD_ERRORS_HPP
12 
13 #include <system_error> // bring in std::error_code et al
14 
15 namespace rad {
16 
17 enum class ErrorCodes {
18  CANCELLED = 0, // Operation cancelled
19  REPLY_TIMEOUT,
20  DESERIALIZATION_ERR,
21  UNKNOWN
22 };
23 
24 class ErrorCategory : public std::error_category {
25  public:
26  const char* name() const noexcept override final { return "RAD Error"; }
27 
28  std::string message(int err_value) const override final {
29  switch (static_cast<rad::ErrorCodes>(err_value)) {
30  case rad::ErrorCodes::REPLY_TIMEOUT:
31  return "Reply timed out.";
32  case rad::ErrorCodes::CANCELLED:
33  return "Operation was cancelled.";
34  case rad::ErrorCodes::DESERIALIZATION_ERR:
35  return "Error parsing message payload.";
37  default:
38  return "Unknown error";
39  }
40  }
41 };
42 
43 extern inline const ErrorCategory& GetErrorCategory() {
44  static rad::ErrorCategory cat;
45  return cat;
46 }
47 
51 // NOLINTNEXTLINE
52 inline std::error_code make_error_code(rad::ErrorCodes e) {
53  return {static_cast<int>(e), rad::GetErrorCategory()};
54 }
55 
56 } // namespace rad
57 
58 namespace std {
59 
60 // Register rad::ErrorCodes in the standard C++11 error code system
61 template <>
62 struct is_error_code_enum<rad::ErrorCodes> : std::true_type {};
63 
64 } // namespace std
65 
66 #endif // RAD_ERRORS_HPP
rad::make_error_code
std::error_code make_error_code(rad::ErrorCodes e)
Definition: errors.hpp:52
rad::ErrorCategory
Definition: errors.hpp:24
rad
Definition: actionCallback.hpp:21
rad::ErrorCodes::CANCELLED
@ CANCELLED
rad::GetErrorCategory
const ErrorCategory & GetErrorCategory()
Definition: errors.hpp:43
rad::ErrorCodes
ErrorCodes
Definition: errors.hpp:17
rad::ErrorCategory::message
std::string message(int err_value) const override final
Definition: errors.hpp:28
std
Definition: errors.hpp:58
rad::ErrorCategory::name
const char * name() const noexcept override final
Definition: errors.hpp:26