summaryrefslogtreecommitdiffstats
path: root/websocketpp/error.hpp
blob: 562fb6ed716c8b52d38e77bda51d06f461b64647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (c) 2014, Peter Thorson. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the WebSocket++ Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef WEBSOCKETPP_ERROR_HPP
#define WEBSOCKETPP_ERROR_HPP

#include <exception>
#include <string>
#include <utility>

#include <websocketpp/common/cpp11.hpp>
#include <websocketpp/common/system_error.hpp>

namespace websocketpp {

/// Combination error code / string type for returning two values
typedef std::pair<lib::error_code,std::string> err_str_pair;

/// Library level error codes
namespace error {
enum value {
    /// Catch-all library error
    general = 1,

    /// send attempted when endpoint write queue was full
    send_queue_full,

    /// Attempted an operation using a payload that was improperly formatted
    /// ex: invalid UTF8 encoding on a text message.
    payload_violation,

    /// Attempted to open a secure connection with an insecure endpoint
    endpoint_not_secure,

    /// Attempted an operation that required an endpoint that is no longer
    /// available. This is usually because the endpoint went out of scope
    /// before a connection that it created.
    endpoint_unavailable,

    /// An invalid uri was supplied
    invalid_uri,

    /// The endpoint is out of outgoing message buffers
    no_outgoing_buffers,

    /// The endpoint is out of incoming message buffers
    no_incoming_buffers,

    /// The connection was in the wrong state for this operation
    invalid_state,

    /// Unable to parse close code
    bad_close_code,

    /// Close code is in a reserved range
    reserved_close_code,

    /// Close code is invalid
    invalid_close_code,

    /// Invalid UTF-8
    invalid_utf8,

    /// Invalid subprotocol
    invalid_subprotocol,

    /// An operation was attempted on a connection that did not exist or was
    /// already deleted.
    bad_connection,

    /// Unit testing utility error code
    test,

    /// Connection creation attempted failed
    con_creation_failed,

    /// Selected subprotocol was not requested by the client
    unrequested_subprotocol,

    /// Attempted to use a client specific feature on a server endpoint
    client_only,

    /// Attempted to use a server specific feature on a client endpoint
    server_only,

    /// HTTP connection ended
    http_connection_ended,

    /// WebSocket opening handshake timed out
    open_handshake_timeout,

    /// WebSocket close handshake timed out
    close_handshake_timeout,

    /// Invalid port in URI
    invalid_port,

    /// An async accept operation failed because the underlying transport has been
    /// requested to not listen for new connections anymore.
    async_accept_not_listening,

    /// The requested operation was canceled
    operation_canceled,

    /// Connection rejected
    rejected,

    /// Upgrade Required. This happens if an HTTP request is made to a
    /// WebSocket++ server that doesn't implement an http handler
    upgrade_required,

    /// Invalid WebSocket protocol version
    invalid_version,

    /// Unsupported WebSocket protocol version
    unsupported_version,

    /// HTTP parse error
    http_parse_error,
    
    /// Extension negotiation failed
    extension_neg_failed
}; // enum value


class category : public lib::error_category {
public:
    category() {}

    char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
        return "websocketpp";
    }

    std::string message(int value) const {
        switch(value) {
            case error::general:
                return "Generic error";
            case error::send_queue_full:
                return "send queue full";
            case error::payload_violation:
                return "payload violation";
            case error::endpoint_not_secure:
                return "endpoint not secure";
            case error::endpoint_unavailable:
                return "endpoint not available";
            case error::invalid_uri:
                return "invalid uri";
            case error::no_outgoing_buffers:
                return "no outgoing message buffers";
            case error::no_incoming_buffers:
                return "no incoming message buffers";
            case error::invalid_state:
                return "invalid state";
            case error::bad_close_code:
                return "Unable to extract close code";
            case error::invalid_close_code:
                return "Extracted close code is in an invalid range";
            case error::reserved_close_code:
                return "Extracted close code is in a reserved range";
            case error::invalid_utf8:
                return "Invalid UTF-8";
            case error::invalid_subprotocol:
                return "Invalid subprotocol";
            case error::bad_connection:
                return "Bad Connection";
            case error::test:
                return "Test Error";
            case error::con_creation_failed:
                return "Connection creation attempt failed";
            case error::unrequested_subprotocol:
                return "Selected subprotocol was not requested by the client";
            case error::client_only:
                return "Feature not available on server endpoints";
            case error::server_only:
                return "Feature not available on client endpoints";
            case error::http_connection_ended:
                return "HTTP connection ended";
            case error::open_handshake_timeout:
                return "The opening handshake timed out";
            case error::close_handshake_timeout:
                return "The closing handshake timed out";
            case error::invalid_port:
                return "Invalid URI port";
            case error::async_accept_not_listening:
                return "Async Accept not listening";
            case error::operation_canceled:
                return "Operation canceled";
            case error::rejected:
                return "Connection rejected";
            case error::upgrade_required:
                return "Upgrade required";
            case error::invalid_version:
                return "Invalid version";
            case error::unsupported_version:
                return "Unsupported version";
            case error::http_parse_error:
                return "HTTP parse error";
            case error::extension_neg_failed:
                return "Extension negotiation failed";
            default:
                return "Unknown";
        }
    }
};

inline const lib::error_category& get_category() {
    static category instance;
    return instance;
}

inline lib::error_code make_error_code(error::value e) {
    return lib::error_code(static_cast<int>(e), get_category());
}

} // namespace error
} // namespace websocketpp

_WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
template<> struct is_error_code_enum<websocketpp::error::value>
{
    static bool const value = true;
};
_WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_

namespace websocketpp {

class exception : public std::exception {
public:
    exception(std::string const & msg, lib::error_code ec = make_error_code(error::general))
      : m_msg(msg.empty() ? ec.message() : msg), m_code(ec)
    {}

    explicit exception(lib::error_code ec)
      : m_msg(ec.message()), m_code(ec)
    {}

    ~exception() throw() {}

    virtual char const * what() const throw() {
        return m_msg.c_str();
    }

    lib::error_code code() const throw() {
        return m_code;
    }

    const std::string m_msg;
    lib::error_code m_code;
};

} // namespace websocketpp

#endif // WEBSOCKETPP_ERROR_HPP