websocketpp  0.3.0
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) 2013, 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 <string>
32 
33 #include <websocketpp/common/cpp11.hpp>
34 #include <websocketpp/common/system_error.hpp>
35 
36 namespace websocketpp {
37 
39 typedef std::pair<lib::error_code,std::string> err_str_pair;
40 
42 namespace error {
43 enum value {
45  general = 1,
46 
49 
53 
56 
61 
64 
67 
70 
73 
76 
79 
82 
85 
88 
92 
95 
98 
101 
104 
107 
110 
113 
116 
119 
123 
126 }; // enum value
127 
128 
129 class category : public lib::error_category {
130 public:
131  category() {}
132 
133  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
134  return "websocketpp";
135  }
136 
137  std::string message(int value) const {
138  switch(value) {
139  case error::general:
140  return "Generic error";
142  return "send queue full";
144  return "payload violation";
146  return "endpoint not secure";
148  return "endpoint not available";
149  case error::invalid_uri:
150  return "invalid uri";
152  return "no outgoing message buffers";
154  return "no incoming message buffers";
156  return "invalid state";
158  return "Unable to extract close code";
160  return "Extracted close code is in an invalid range";
162  return "Extracted close code is in a reserved range";
163  case error::invalid_utf8:
164  return "Invalid UTF-8";
166  return "Invalid subprotocol";
168  return "Bad Connection";
169  case error::test:
170  return "Test Error";
172  return "Connection creation attempt failed";
174  return "Selected subprotocol was not requested by the client";
175  case error::client_only:
176  return "Feature not available on server endpoints";
177  case error::server_only:
178  return "Feature not available on client endpoints";
180  return "HTTP connection ended";
182  return "The opening handshake timed out";
184  return "The closing handshake timed out";
185  case error::invalid_port:
186  return "Invalid URI port";
188  return "Async Accept not listening";
190  return "Operation canceled";
191  default:
192  return "Unknown";
193  }
194  }
195 };
196 
197 inline const lib::error_category& get_category() {
198  static category instance;
199  return instance;
200 }
201 
202 inline lib::error_code make_error_code(error::value e) {
203  return lib::error_code(static_cast<int>(e), get_category());
204 }
205 
206 } // namespace error
207 } // namespace websocketpp
208 
209 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
210 template<> struct is_error_code_enum<websocketpp::error::value>
211 {
212  static bool const value = true;
213 };
214 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
215 
216 namespace websocketpp {
217 
218 class exception : public std::exception {
219 public:
220  exception(std::string const & msg, error::value p_code = error::general)
221  : m_msg(msg), m_code(p_code) {}
222 
223  ~exception() throw() {}
224 
225  virtual char const * what() const throw() {
226  return m_msg.c_str();
227  }
228 
229  error::value code() const throw() {
230  return m_code;
231  }
232 
233  std::string m_msg;
234  error::value m_code;
235 };
236 
237 } // namespace websocketpp
238 
239 #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:103
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Definition: error.hpp:39
Selected subprotocol was not requested by the client.
Definition: error.hpp:100
Invalid port in URI.
Definition: error.hpp:118
Close code is invalid.
Definition: error.hpp:81
Unable to parse close code.
Definition: error.hpp:75
Close code is in a reserved range.
Definition: error.hpp:78
The connection was in the wrong state for this operation.
Definition: error.hpp:72
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
The requested operation was canceled.
Definition: error.hpp:125
The endpoint is out of incoming message buffers.
Definition: error.hpp:69
send attempted when endpoint write queue was full
Definition: error.hpp:48
The endpoint is out of outgoing message buffers.
Definition: error.hpp:66
WebSocket close handshake timed out.
Definition: error.hpp:115
Catch-all library error.
Definition: error.hpp:45
WebSocket opening handshake timed out.
Definition: error.hpp:112
Attempted to use a server specific feature on a client endpoint.
Definition: error.hpp:106
Attempted to open a secure connection with an insecure endpoint.
Definition: error.hpp:55
Unit testing utility error code.
Definition: error.hpp:94
An invalid uri was supplied.
Definition: error.hpp:63
Connection creation attempted failed.
Definition: error.hpp:97