WebSocket++  0.8.3-dev
C++ websocket client/server library
error.hpp
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 
28 #ifndef WEBSOCKETPP_ERROR_HPP
29 #define WEBSOCKETPP_ERROR_HPP
30 
31 #include <exception>
32 #include <string>
33 #include <utility>
34 
35 #include <websocketpp/common/cpp11.hpp>
36 #include <websocketpp/common/system_error.hpp>
37 
38 namespace websocketpp {
39 
40 /// Combination error code / string type for returning two values
42 
43 /// Library level error codes
44 namespace error {
45 enum value {
46  /// Catch-all library error
47  general = 1,
48 
49  /// send attempted when endpoint write queue was full
51 
52  /// Attempted an operation using a payload that was improperly formatted
53  /// ex: invalid UTF8 encoding on a text message.
55 
56  /// Attempted to open a secure connection with an insecure endpoint
58 
59  /// Attempted an operation that required an endpoint that is no longer
60  /// available. This is usually because the endpoint went out of scope
61  /// before a connection that it created.
63 
64  /// An invalid uri was supplied
66 
67  /// The endpoint is out of outgoing message buffers
69 
70  /// The endpoint is out of incoming message buffers
72 
73  /// The connection was in the wrong state for this operation
75 
76  /// Unable to parse close code
78 
79  /// Close code is in a reserved range
81 
82  /// Close code is invalid
84 
85  /// Invalid UTF-8
87 
88  /// Invalid subprotocol
90 
91  /// An operation was attempted on a connection that did not exist or was
92  /// already deleted.
94 
95  /// Unit testing utility error code
97 
98  /// Connection creation attempted failed
100 
101  /// Selected subprotocol was not requested by the client
103 
104  /// Attempted to use a client specific feature on a server endpoint
106 
107  /// Attempted to use a server specific feature on a client endpoint
109 
110  /// HTTP connection ended
112 
113  /// WebSocket opening handshake timed out
115 
116  /// WebSocket close handshake timed out
118 
119  /// Invalid port in URI
121 
122  /// An async accept operation failed because the underlying transport has been
123  /// requested to not listen for new connections anymore.
125 
126  /// The requested operation was canceled
128 
129  /// Connection rejected
131 
132  /// Upgrade Required. This happens if an HTTP request is made to a
133  /// WebSocket++ server that doesn't implement an http handler
135 
136  /// Invalid WebSocket protocol version
138 
139  /// Unsupported WebSocket protocol version
141 
142  /// HTTP parse error
144 
145  /// Extension negotiation failed
147 }; // enum value
148 
149 
150 class category : public lib::error_category {
151 public:
152  category() {}
153 
154  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
155  return "websocketpp";
156  }
157 
158  std::string message(int value) const {
159  switch(value) {
160  case error::general:
161  return "Generic error";
162  case error::send_queue_full:
163  return "send queue full";
164  case error::payload_violation:
165  return "payload violation";
166  case error::endpoint_not_secure:
167  return "endpoint not secure";
168  case error::endpoint_unavailable:
169  return "endpoint not available";
170  case error::invalid_uri:
171  return "invalid uri";
172  case error::no_outgoing_buffers:
173  return "no outgoing message buffers";
174  case error::no_incoming_buffers:
175  return "no incoming message buffers";
176  case error::invalid_state:
177  return "invalid state";
178  case error::bad_close_code:
179  return "Unable to extract close code";
180  case error::invalid_close_code:
181  return "Extracted close code is in an invalid range";
182  case error::reserved_close_code:
183  return "Extracted close code is in a reserved range";
184  case error::invalid_utf8:
185  return "Invalid UTF-8";
186  case error::invalid_subprotocol:
187  return "Invalid subprotocol";
188  case error::bad_connection:
189  return "Bad Connection";
190  case error::test:
191  return "Test Error";
192  case error::con_creation_failed:
193  return "Connection creation attempt failed";
194  case error::unrequested_subprotocol:
195  return "Selected subprotocol was not requested by the client";
196  case error::client_only:
197  return "Feature not available on server endpoints";
198  case error::server_only:
199  return "Feature not available on client endpoints";
200  case error::http_connection_ended:
201  return "HTTP connection ended";
202  case error::open_handshake_timeout:
203  return "The opening handshake timed out";
204  case error::close_handshake_timeout:
205  return "The closing handshake timed out";
206  case error::invalid_port:
207  return "Invalid URI port";
208  case error::async_accept_not_listening:
209  return "Async Accept not listening";
210  case error::operation_canceled:
211  return "Operation canceled";
212  case error::rejected:
213  return "Connection rejected";
214  case error::upgrade_required:
215  return "Upgrade required";
216  case error::invalid_version:
217  return "Invalid version";
218  case error::unsupported_version:
219  return "Unsupported version";
220  case error::http_parse_error:
221  return "HTTP parse error";
222  case error::extension_neg_failed:
223  return "Extension negotiation failed";
224  default:
225  return "Unknown";
226  }
227  }
228 };
229 
230 inline const lib::error_category& get_category() {
231  static category instance;
232  return instance;
233 }
234 
235 inline lib::error_code make_error_code(error::value e) {
236  return lib::error_code(static_cast<int>(e), get_category());
237 }
238 
239 } // namespace error
240 } // namespace websocketpp
241 
242 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
243 template<> struct is_error_code_enum<websocketpp::error::value>
244 {
245  static bool const value = true;
246 };
247 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
248 
249 namespace websocketpp {
250 
251 class exception : public std::exception {
252 public:
253  exception(std::string const & msg, lib::error_code ec = make_error_code(error::general))
254  : m_msg(msg.empty() ? ec.message() : msg), m_code(ec)
255  {}
256 
257  explicit exception(lib::error_code ec)
258  : m_msg(ec.message()), m_code(ec)
259  {}
260 
261  ~exception() throw() {}
262 
263  virtual char const * what() const throw() {
264  return m_msg.c_str();
265  }
266 
267  lib::error_code code() const throw() {
268  return m_code;
269  }
270 
271  const std::string m_msg;
272  lib::error_code m_code;
273 };
274 
275 } // namespace websocketpp
276 
277 #endif // WEBSOCKETPP_ERROR_HPP
websocketpp::error::unrequested_subprotocol
@ unrequested_subprotocol
Selected subprotocol was not requested by the client.
Definition: error.hpp:102
websocketpp::error::client_only
@ client_only
Attempted to use a client specific feature on a server endpoint.
Definition: error.hpp:105
websocketpp::err_str_pair
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Definition: error.hpp:41
websocketpp::error::bad_connection
@ bad_connection
Definition: error.hpp:93
websocketpp::error
Library level error codes.
Definition: error.hpp:44
websocketpp::error::unsupported_version
@ unsupported_version
Unsupported WebSocket protocol version.
Definition: error.hpp:140
websocketpp::exception
Definition: error.hpp:251
websocketpp::error::endpoint_not_secure
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
Definition: error.hpp:57
websocketpp::error::send_queue_full
@ send_queue_full
send attempted when endpoint write queue was full
Definition: error.hpp:50
websocketpp::error::http_connection_ended
@ http_connection_ended
HTTP connection ended.
Definition: error.hpp:111
websocketpp::error::con_creation_failed
@ con_creation_failed
Connection creation attempted failed.
Definition: error.hpp:99
websocketpp::error::invalid_uri
@ invalid_uri
An invalid uri was supplied.
Definition: error.hpp:65
websocketpp::error::test
@ test
Unit testing utility error code.
Definition: error.hpp:96
websocketpp::error::close_handshake_timeout
@ close_handshake_timeout
WebSocket close handshake timed out.
Definition: error.hpp:117
websocketpp::error::upgrade_required
@ upgrade_required
Definition: error.hpp:134
websocketpp::error::invalid_version
@ invalid_version
Invalid WebSocket protocol version.
Definition: error.hpp:137
websocketpp::versions_supported
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
websocketpp::error::rejected
@ rejected
Connection rejected.
Definition: error.hpp:130
websocketpp::error::payload_violation
@ payload_violation
Definition: error.hpp:54
websocketpp::error::no_outgoing_buffers
@ no_outgoing_buffers
The endpoint is out of outgoing message buffers.
Definition: error.hpp:68
websocketpp::error::general
@ general
Catch-all library error.
Definition: error.hpp:47
websocketpp::error::extension_neg_failed
@ extension_neg_failed
Extension negotiation failed.
Definition: error.hpp:146
websocketpp::error::invalid_subprotocol
@ invalid_subprotocol
Invalid subprotocol.
Definition: error.hpp:89
websocketpp::error::server_only
@ server_only
Attempted to use a server specific feature on a client endpoint.
Definition: error.hpp:108
websocketpp::error::open_handshake_timeout
@ open_handshake_timeout
WebSocket opening handshake timed out.
Definition: error.hpp:114
websocketpp::error::bad_close_code
@ bad_close_code
Unable to parse close code.
Definition: error.hpp:77
websocketpp::error::invalid_utf8
@ invalid_utf8
Invalid UTF-8.
Definition: error.hpp:86
websocketpp::error::value
value
Definition: error.hpp:45
websocketpp::error::no_incoming_buffers
@ no_incoming_buffers
The endpoint is out of incoming message buffers.
Definition: error.hpp:71
websocketpp::error::async_accept_not_listening
@ async_accept_not_listening
Definition: error.hpp:124
websocketpp::error::http_parse_error
@ http_parse_error
HTTP parse error.
Definition: error.hpp:143
websocketpp::error::operation_canceled
@ operation_canceled
The requested operation was canceled.
Definition: error.hpp:127
websocketpp::error::invalid_state
@ invalid_state
The connection was in the wrong state for this operation.
Definition: error.hpp:74
websocketpp::error::reserved_close_code
@ reserved_close_code
Close code is in a reserved range.
Definition: error.hpp:80
websocketpp::error::invalid_port
@ invalid_port
Invalid port in URI.
Definition: error.hpp:120
websocketpp::error::invalid_close_code
@ invalid_close_code
Close code is invalid.
Definition: error.hpp:83
websocketpp::error::endpoint_unavailable
@ endpoint_unavailable
Definition: error.hpp:62