websocketpp  0.5.1
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 
41 typedef std::pair<lib::error_code,std::string> err_str_pair;
42 
44 namespace error {
45 enum value {
47  general = 1,
48 
51 
55 
58 
63 
66 
69 
72 
75 
78 
81 
84 
87 
90 
94 
97 
100 
103 
106 
109 
112 
115 
118 
121 
125 
128 
131 
135 
138 
141 
144 }; // enum value
145 
146 
147 class category : public lib::error_category {
148 public:
149  category() {}
150 
151  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
152  return "websocketpp";
153  }
154 
155  std::string message(int value) const {
156  switch(value) {
157  case error::general:
158  return "Generic error";
160  return "send queue full";
162  return "payload violation";
164  return "endpoint not secure";
166  return "endpoint not available";
167  case error::invalid_uri:
168  return "invalid uri";
170  return "no outgoing message buffers";
172  return "no incoming message buffers";
174  return "invalid state";
176  return "Unable to extract close code";
178  return "Extracted close code is in an invalid range";
180  return "Extracted close code is in a reserved range";
181  case error::invalid_utf8:
182  return "Invalid UTF-8";
184  return "Invalid subprotocol";
186  return "Bad Connection";
187  case error::test:
188  return "Test Error";
190  return "Connection creation attempt failed";
192  return "Selected subprotocol was not requested by the client";
193  case error::client_only:
194  return "Feature not available on server endpoints";
195  case error::server_only:
196  return "Feature not available on client endpoints";
198  return "HTTP connection ended";
200  return "The opening handshake timed out";
202  return "The closing handshake timed out";
203  case error::invalid_port:
204  return "Invalid URI port";
206  return "Async Accept not listening";
208  return "Operation canceled";
209  case error::rejected:
210  return "Connection rejected";
212  return "Upgrade required";
214  return "Invalid version";
216  return "Unsupported version";
218  return "HTTP parse error";
219  default:
220  return "Unknown";
221  }
222  }
223 };
224 
225 inline const lib::error_category& get_category() {
226  static category instance;
227  return instance;
228 }
229 
230 inline lib::error_code make_error_code(error::value e) {
231  return lib::error_code(static_cast<int>(e), get_category());
232 }
233 
234 } // namespace error
235 } // namespace websocketpp
236 
237 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
238 template<> struct is_error_code_enum<websocketpp::error::value>
239 {
240  static bool const value = true;
241 };
242 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
243 
244 namespace websocketpp {
245 
246 class exception : public std::exception {
247 public:
248  exception(std::string const & msg, lib::error_code ec = make_error_code(error::general))
249  : m_msg(msg.empty() ? ec.message() : msg), m_code(ec)
250  {}
251 
252  explicit exception(lib::error_code ec)
253  : m_code(ec)
254  {}
255 
256  ~exception() throw() {}
257 
258  virtual char const * what() const throw() {
259  return m_msg.c_str();
260  }
261 
262  lib::error_code code() const throw() {
263  return m_code;
264  }
265 
266  std::string m_msg;
267  lib::error_code m_code;
268 };
269 
270 } // namespace websocketpp
271 
272 #endif // WEBSOCKETPP_ERROR_HPP
uint16_t value
The type of a close code value.
Definition: close.hpp:49
Attempted to use a client specific feature on a server endpoint.
Definition: error.hpp:105
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Definition: error.hpp:41
Connection rejected.
Definition: error.hpp:130
Selected subprotocol was not requested by the client.
Definition: error.hpp:102
Invalid port in URI.
Definition: error.hpp:120
Close code is invalid.
Definition: error.hpp:83
Unable to parse close code.
Definition: error.hpp:77
Close code is in a reserved range.
Definition: error.hpp:80
Invalid WebSocket protocol version.
Definition: error.hpp:137
The connection was in the wrong state for this operation.
Definition: error.hpp:74
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
The requested operation was canceled.
Definition: error.hpp:127
The endpoint is out of incoming message buffers.
Definition: error.hpp:71
send attempted when endpoint write queue was full
Definition: error.hpp:50
The endpoint is out of outgoing message buffers.
Definition: error.hpp:68
WebSocket close handshake timed out.
Definition: error.hpp:117
Catch-all library error.
Definition: error.hpp:47
WebSocket opening handshake timed out.
Definition: error.hpp:114
Attempted to use a server specific feature on a client endpoint.
Definition: error.hpp:108
Attempted to open a secure connection with an insecure endpoint.
Definition: error.hpp:57
Unit testing utility error code.
Definition: error.hpp:96
An invalid uri was supplied.
Definition: error.hpp:65
Unsupported WebSocket protocol version.
Definition: error.hpp:140
Connection creation attempted failed.
Definition: error.hpp:99