You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_ERROR_HPP
  28. #define WEBSOCKETPP_ERROR_HPP
  29. #include <exception>
  30. #include <string>
  31. #include <utility>
  32. #include <websocketpp/common/cpp11.hpp>
  33. #include <websocketpp/common/system_error.hpp>
  34. namespace websocketpp
  35. {
  36. /// Combination error code / string type for returning two values
  37. typedef std::pair<lib::error_code, std::string> err_str_pair;
  38. /// Library level error codes
  39. namespace error
  40. {
  41. enum value
  42. {
  43. /// Catch-all library error
  44. general = 1,
  45. /// send attempted when endpoint write queue was full
  46. send_queue_full,
  47. /// Attempted an operation using a payload that was improperly formatted
  48. /// ex: invalid UTF8 encoding on a text message.
  49. payload_violation,
  50. /// Attempted to open a secure connection with an insecure endpoint
  51. endpoint_not_secure,
  52. /// Attempted an operation that required an endpoint that is no longer
  53. /// available. This is usually because the endpoint went out of scope
  54. /// before a connection that it created.
  55. endpoint_unavailable,
  56. /// An invalid uri was supplied
  57. invalid_uri,
  58. /// The endpoint is out of outgoing message buffers
  59. no_outgoing_buffers,
  60. /// The endpoint is out of incoming message buffers
  61. no_incoming_buffers,
  62. /// The connection was in the wrong state for this operation
  63. invalid_state,
  64. /// Unable to parse close code
  65. bad_close_code,
  66. /// Close code is in a reserved range
  67. reserved_close_code,
  68. /// Close code is invalid
  69. invalid_close_code,
  70. /// Invalid UTF-8
  71. invalid_utf8,
  72. /// Invalid subprotocol
  73. invalid_subprotocol,
  74. /// An operation was attempted on a connection that did not exist or was
  75. /// already deleted.
  76. bad_connection,
  77. /// Unit testing utility error code
  78. test,
  79. /// Connection creation attempted failed
  80. con_creation_failed,
  81. /// Selected subprotocol was not requested by the client
  82. unrequested_subprotocol,
  83. /// Attempted to use a client specific feature on a server endpoint
  84. client_only,
  85. /// Attempted to use a server specific feature on a client endpoint
  86. server_only,
  87. /// HTTP connection ended
  88. http_connection_ended,
  89. /// WebSocket opening handshake timed out
  90. open_handshake_timeout,
  91. /// WebSocket close handshake timed out
  92. close_handshake_timeout,
  93. /// Invalid port in URI
  94. invalid_port,
  95. /// An async accept operation failed because the underlying transport has been
  96. /// requested to not listen for new connections anymore.
  97. async_accept_not_listening,
  98. /// The requested operation was canceled
  99. operation_canceled,
  100. /// Connection rejected
  101. rejected,
  102. /// Upgrade Required. This happens if an HTTP request is made to a
  103. /// WebSocket++ server that doesn't implement an http handler
  104. upgrade_required,
  105. /// Invalid WebSocket protocol version
  106. invalid_version,
  107. /// Unsupported WebSocket protocol version
  108. unsupported_version,
  109. /// HTTP parse error
  110. http_parse_error,
  111. /// Extension negotiation failed
  112. extension_neg_failed
  113. }; // enum value
  114. class category : public lib::error_category
  115. {
  116. public:
  117. category() {}
  118. const char * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
  119. {
  120. return "websocketpp";
  121. }
  122. std::string message(int value) const
  123. {
  124. switch (value)
  125. {
  126. case error::general : return "Generic error";
  127. case error::send_queue_full : return "send queue full";
  128. case error::payload_violation : return "payload violation";
  129. case error::endpoint_not_secure : return "endpoint not secure";
  130. case error::endpoint_unavailable : return "endpoint not available";
  131. case error::invalid_uri : return "invalid uri";
  132. case error::no_outgoing_buffers : return "no outgoing message buffers";
  133. case error::no_incoming_buffers : return "no incoming message buffers";
  134. case error::invalid_state : return "invalid state";
  135. case error::bad_close_code : return "Unable to extract close code";
  136. case error::invalid_close_code : return "Extracted close code is in an invalid range";
  137. case error::reserved_close_code : return "Extracted close code is in a reserved range";
  138. case error::invalid_utf8 : return "Invalid UTF-8";
  139. case error::invalid_subprotocol : return "Invalid subprotocol";
  140. case error::bad_connection : return "Bad Connection";
  141. case error::test : return "Test Error";
  142. case error::con_creation_failed : return "Connection creation attempt failed";
  143. case error::unrequested_subprotocol : return "Selected subprotocol was not requested by the client";
  144. case error::client_only : return "Feature not available on server endpoints";
  145. case error::server_only : return "Feature not available on client endpoints";
  146. case error::http_connection_ended : return "HTTP connection ended";
  147. case error::open_handshake_timeout : return "The opening handshake timed out";
  148. case error::close_handshake_timeout : return "The closing handshake timed out";
  149. case error::invalid_port : return "Invalid URI port";
  150. case error::async_accept_not_listening: return "Async Accept not listening";
  151. case error::operation_canceled : return "Operation canceled";
  152. case error::rejected : return "Connection rejected";
  153. case error::upgrade_required : return "Upgrade required";
  154. case error::invalid_version : return "Invalid version";
  155. case error::unsupported_version : return "Unsupported version";
  156. case error::http_parse_error : return "HTTP parse error";
  157. case error::extension_neg_failed : return "Extension negotiation failed";
  158. default : return "Unknown";
  159. }
  160. }
  161. };
  162. inline const lib::error_category & get_category()
  163. {
  164. static category instance;
  165. return instance;
  166. }
  167. inline lib::error_code make_error_code(error::value e)
  168. {
  169. return lib::error_code(static_cast<int>(e), get_category());
  170. }
  171. } // namespace error
  172. } // namespace websocketpp
  173. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
  174. template <> struct is_error_code_enum<websocketpp::error::value>
  175. {
  176. static const bool value = true;
  177. };
  178. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
  179. namespace websocketpp
  180. {
  181. class exception : public std::exception
  182. {
  183. public:
  184. exception(const std::string & msg, lib::error_code ec = make_error_code(error::general)) :
  185. m_msg(msg.empty() ? ec.message() : msg),
  186. m_code(ec)
  187. {
  188. }
  189. explicit exception(lib::error_code ec) :
  190. m_msg(ec.message()),
  191. m_code(ec)
  192. {
  193. }
  194. ~exception() throw() {}
  195. virtual const char * what() const throw()
  196. {
  197. return m_msg.c_str();
  198. }
  199. lib::error_code code() const throw()
  200. {
  201. return m_code;
  202. }
  203. const std::string m_msg;
  204. lib::error_code m_code;
  205. };
  206. } // namespace websocketpp
  207. #endif // WEBSOCKETPP_ERROR_HPP