error.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_RPC_ERROR_HPP__
  11. #define __ASIO2_RPC_ERROR_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/error.hpp>
  16. namespace asio2::rpc
  17. {
  18. /**
  19. */
  20. enum class error
  21. {
  22. // The operation completed successfully.
  23. success = 0,
  24. // Already open.
  25. already_open = 1,
  26. // End of file or stream.
  27. eof,
  28. // Element not found.
  29. not_found,
  30. // Operation aborted.
  31. operation_aborted,
  32. // Invalid argument.
  33. invalid_argument,
  34. // Illegal data.
  35. illegal_data,
  36. // The operation timed out.
  37. timed_out,
  38. // Does not have associated data.
  39. no_data,
  40. // Operation now in progress.
  41. in_progress,
  42. // Transport endpoint is not connected.
  43. not_connected,
  44. // Unspecified error.
  45. unspecified_error,
  46. };
  47. enum class condition
  48. {
  49. // The operation completed successfully.
  50. success = 0,
  51. // Already open.
  52. already_open = 1,
  53. // End of file or stream.
  54. eof,
  55. // Element not found.
  56. not_found,
  57. // Operation aborted.
  58. operation_aborted,
  59. // Invalid argument.
  60. invalid_argument,
  61. // Illegal data.
  62. illegal_data,
  63. // The operation timed out.
  64. timed_out,
  65. // Does not have associated data.
  66. no_data,
  67. // Operation now in progress.
  68. in_progress,
  69. // Transport endpoint is not connected.
  70. not_connected,
  71. // Unspecified error.
  72. unspecified_error,
  73. };
  74. /// The type of error category used by the library
  75. using error_category = asio::error_category;
  76. /// The type of error condition used by the library
  77. using error_condition = asio::error_condition;
  78. class rpc_error_category : public error_category
  79. {
  80. public:
  81. const char* name() const noexcept override
  82. {
  83. return "asio2.rpc";
  84. }
  85. inline std::string message(int ev) const override
  86. {
  87. switch (static_cast<error>(ev))
  88. {
  89. case error::success : return "The operation completed successfully.";
  90. case error::already_open : return "Already open.";
  91. case error::eof : return "End of file or stream.";
  92. case error::not_found : return "Element not found.";
  93. case error::operation_aborted : return "Operation aborted.";
  94. case error::invalid_argument : return "Invalid argument.";
  95. case error::illegal_data : return "Illegal data.";
  96. case error::timed_out : return "The operation timed out.";
  97. case error::no_data : return "Does not have associated data.";
  98. case error::in_progress : return "Operation now in progress.";
  99. case error::not_connected : return "Transport endpoint is not connected.";
  100. case error::unspecified_error : return "Unspecified error.";
  101. default : return "Unknown error";
  102. }
  103. }
  104. inline error_condition default_error_condition(int ev) const noexcept override
  105. {
  106. return error_condition{ ev, *this };
  107. }
  108. };
  109. inline const rpc_error_category& rpc_category() noexcept
  110. {
  111. static rpc_error_category const cat{};
  112. return cat;
  113. }
  114. inline asio::error_code make_error_code(error e)
  115. {
  116. return asio::error_code{ static_cast<std::underlying_type<error>::type>(e), rpc_category() };
  117. }
  118. inline error_condition make_error_condition(condition c)
  119. {
  120. return error_condition{ static_cast<std::underlying_type<condition>::type>(c), rpc_category() };
  121. }
  122. template<typename = void>
  123. inline constexpr std::string_view to_string(error e)
  124. {
  125. using namespace std::string_view_literals;
  126. switch (e)
  127. {
  128. case error::success : return "The operation completed successfully.";
  129. case error::operation_aborted : return "Operation aborted.";
  130. case error::invalid_argument : return "Invalid argument.";
  131. case error::illegal_data : return "Illegal data.";
  132. case error::timed_out : return "The operation timed out.";
  133. case error::no_data : return "Does not have associated data.";
  134. case error::in_progress : return "Operation now in progress.";
  135. case error::not_connected : return "Transport endpoint is not connected.";
  136. case error::not_found : return "Element not found.";
  137. case error::unspecified_error : return "Unspecified error.";
  138. default : return "Unknown error";
  139. }
  140. return "Unknown error";
  141. };
  142. }
  143. namespace rpc = ::asio2::rpc;
  144. namespace std
  145. {
  146. template<>
  147. struct is_error_code_enum<::asio2::rpc::error>
  148. {
  149. static bool const value = true;
  150. };
  151. template<>
  152. struct is_error_condition_enum<::asio2::rpc::condition>
  153. {
  154. static bool const value = true;
  155. };
  156. }
  157. #endif // !__ASIO2_RPC_ERROR_HPP__