Tidy up error types:

* Restructure header material
* Clean up namespace type injections
* Remove extraneous 'success' constants
This commit is contained in:
Vinnie Falco
2016-10-04 18:00:11 -04:00
parent f2a283ed10
commit 6b0b016b21
23 changed files with 260 additions and 292 deletions

View File

@ -5,6 +5,7 @@
* Add Secure WebSocket example
* Fix message_v1 constructor
* Tidy up DynamicBuffer requirements
* Tidy up error types and headers
--------------------------------------------------------------------------------

View File

@ -139,7 +139,10 @@
<member><link linkend="beast.ref.buffers_adapter">buffers_adapter</link></member>
<member><link linkend="beast.ref.consuming_buffers">consuming_buffers</link></member>
<member><link linkend="beast.ref.dynabuf_readstream">dynabuf_readstream</link></member>
<member><link linkend="beast.ref.errc">errc</link></member>
<member><link linkend="beast.ref.error_category">error_category</link></member>
<member><link linkend="beast.ref.error_code">error_code</link></member>
<member><link linkend="beast.ref.error_condition">error_condition</link></member>
<member><link linkend="beast.ref.handler_alloc">handler_alloc</link></member>
<member><link linkend="beast.ref.prepared_buffers">prepared_buffers</link></member>
<member><link linkend="beast.ref.static_streambuf">static_streambuf</link></member>

View File

@ -49,7 +49,7 @@ int main(int, char const*[])
beast::http::read(sock, sb, res);
std::cout << res;
}
catch(boost::system::system_error const& ec)
catch(beast::system_error const& ec)
{
std::cerr << host << ": " << ec.what();
}

View File

@ -13,11 +13,9 @@
namespace beast {
namespace test {
enum error
enum class error
{
success = 0,
fail_error
fail_error = 1
};
namespace detail {
@ -79,14 +77,15 @@ inline
error_code
make_error_code(error ev)
{
return error_code{static_cast<int>(ev),
detail::get_error_category()};
return error_code{
static_cast<std::underlying_type<error>::type>(ev),
detail::get_error_category()};
}
/** A countdown to simulated failure.
On the Nth operation, the class will fail with the specified
error code, or the default error code of @ref fail_error.
error code, or the default error code of @ref error::fail_error.
*/
class fail_counter
{
@ -102,7 +101,7 @@ public:
*/
explicit
fail_counter(std::size_t n,
error_code ev = make_error_code(fail_error))
error_code ev = make_error_code(error::fail_error))
: n_(n)
, ec_(ev)
{

View File

@ -34,11 +34,11 @@ namespace beast {
...
template<class CompletionHandler>
typename async_completion<CompletionHandler,
void(boost::system::error_code)>::result_type
void(error_code)>::result_type
async_initfn(..., CompletionHandler&& handler)
{
async_completion<CompletionHandler,
void(boost::system::error_code)> completion(handler);
void(error_code)> completion(handler);
...
return completion.result.get();
}

View File

@ -9,6 +9,7 @@
#define BEAST_DETAIL_STREAM_CONCEPTS_HPP
#include <beast/core/buffer_concepts.hpp>
#include <beast/core/error.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/system/error_code.hpp>
#include <type_traits>
@ -22,7 +23,7 @@ namespace detail {
struct StreamHandler
{
StreamHandler(StreamHandler const&) = default;
void operator()(boost::system::error_code ec, std::size_t);
void operator()(error_code ec, std::size_t);
};
using ReadHandler = StreamHandler;
using WriteHandler = StreamHandler;
@ -79,9 +80,6 @@ public:
template<class T>
class is_SyncReadStream
{
using error_code =
boost::system::error_code;
template<class U, class R = std::is_same<decltype(
std::declval<U>().read_some(
std::declval<MutableBufferSequence>())),
@ -108,9 +106,6 @@ public:
template<class T>
class is_SyncWriteStream
{
using error_code =
boost::system::error_code;
template<class U, class R = std::is_same<decltype(
std::declval<U>().write_some(
std::declval<ConstBufferSequence>())),

View File

@ -19,6 +19,18 @@ using error_code = boost::system::error_code;
/// The type of system error thrown by the library
using system_error = boost::system::system_error;
/// The type of error category used by the library
using error_category = boost::system::error_category;
/// The type of error condition used by the library
using error_condition = boost::system::error_condition;
/// The set of constants used for cross-platform error codes
#if GENERATING_DOCS
enum errc{};
#else
namespace errc = boost::system::errc;
#endif
} // beast
#endif

View File

@ -0,0 +1,107 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_HTTP_IMPL_PARSE_ERROR_IPP
#define BEAST_HTTP_IMPL_PARSE_ERROR_IPP
namespace boost {
namespace system {
template<>
struct is_error_code_enum<beast::http::parse_error>
{
static bool const value = true;
};
} // system
} // boost
namespace beast {
namespace http {
namespace detail {
class parse_error_category : public error_category
{
public:
const char*
name() const noexcept override
{
return "http";
}
std::string
message(int ev) const override
{
switch(static_cast<parse_error>(ev))
{
case parse_error::connection_closed: return "data after Connection close";
case parse_error::bad_method: return "bad method";
case parse_error::bad_uri: return "bad request-target";
case parse_error::bad_version: return "bad HTTP-Version";
case parse_error::bad_crlf: return "missing CRLF";
case parse_error::bad_status: return "bad status-code";
case parse_error::bad_reason: return "bad reason-phrase";
case parse_error::bad_field: return "bad field token";
case parse_error::bad_value: return "bad field-value";
case parse_error::bad_content_length: return "bad Content-Length";
case parse_error::illegal_content_length: return "illegal Content-Length with chunked Transfer-Encoding";
case parse_error::bad_on_headers_rv: return "on_headers returned an unknown value";
case parse_error::invalid_chunk_size: return "invalid chunk size";
case parse_error::invalid_ext_name: return "invalid ext name";
case parse_error::invalid_ext_val: return "invalid ext val";
case parse_error::headers_too_big: return "headers size limit exceeded";
case parse_error::body_too_big: return "body size limit exceeded";
case parse_error::short_read: return "unexpected end of data";
default:
return "parse error";
}
}
error_condition
default_error_condition(int ev) const noexcept override
{
return error_condition{ev, *this};
}
bool
equivalent(int ev,
error_condition const& condition
) const noexcept override
{
return condition.value() == ev &&
&condition.category() == this;
}
bool
equivalent(error_code const& error, int ev) const noexcept override
{
return error.value() == ev &&
&error.category() == this;
}
};
inline
error_category const&
get_parse_error_category()
{
static parse_error_category const cat{};
return cat;
}
} // detail
inline
error_code
make_error_code(parse_error ev)
{
return error_code{
static_cast<std::underlying_type<parse_error>::type>(ev),
detail::get_parse_error_category()};
}
} // http
} // beast
#endif

View File

@ -332,7 +332,7 @@ parse(SyncReadStream& stream,
error_code ec;
parse(stream, dynabuf, parser, ec);
if(ec)
throw boost::system::system_error{ec};
throw system_error{ec};
}
template<class SyncReadStream, class DynamicBuffer, class Parser>

View File

@ -462,7 +462,7 @@ template<class SyncWriteStream,
void
write(SyncWriteStream& stream,
message_v1<isRequest, Body, Headers> const& msg,
boost::system::error_code& ec)
error_code& ec)
{
static_assert(is_SyncWriteStream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@ -596,8 +596,8 @@ public:
buffer_size(buffer));
if(os_.fail())
{
ec = boost::system::errc::make_error_code(
boost::system::errc::no_stream_resources);
ec = errc::make_error_code(
errc::no_stream_resources);
break;
}
n += buffer_size(buffer);

View File

@ -15,15 +15,12 @@ namespace http {
enum class parse_error
{
success = 0,
connection_closed,
connection_closed = 1,
bad_method,
bad_uri,
bad_version,
bad_crlf,
bad_request,
bad_status,
bad_reason,
@ -45,132 +42,9 @@ enum class parse_error
general
};
class parse_error_category : public boost::system::error_category
{
public:
const char*
name() const noexcept override
{
return "http";
}
std::string
message(int ev) const override
{
switch(static_cast<parse_error>(ev))
{
case parse_error::connection_closed:
return "data after Connection close";
case parse_error::bad_method:
return "bad method";
case parse_error::bad_uri:
return "bad request-target";
case parse_error::bad_version:
return "bad HTTP-Version";
case parse_error::bad_crlf:
return "missing CRLF";
case parse_error::bad_request:
return "bad reason-phrase";
case parse_error::bad_status:
return "bad status-code";
case parse_error::bad_reason:
return "bad reason-phrase";
case parse_error::bad_field:
return "bad field token";
case parse_error::bad_value:
return "bad field-value";
case parse_error::bad_content_length:
return "bad Content-Length";
case parse_error::illegal_content_length:
return "illegal Content-Length with chunked Transfer-Encoding";
case parse_error::bad_on_headers_rv:
return "on_headers returned an unknown value";
case parse_error::invalid_chunk_size:
return "invalid chunk size";
case parse_error::invalid_ext_name:
return "invalid ext name";
case parse_error::invalid_ext_val:
return "invalid ext val";
case parse_error::headers_too_big:
return "headers size limit exceeded";
case parse_error::body_too_big:
return "body size limit exceeded";
case parse_error::short_read:
return "unexpected end of data";
default:
return "parse error";
}
}
boost::system::error_condition
default_error_condition(int ev) const noexcept override
{
return boost::system::error_condition(ev, *this);
}
bool
equivalent(int ev,
boost::system::error_condition const& condition
) const noexcept override
{
return condition.value() == ev &&
&condition.category() == this;
}
bool
equivalent(error_code const& error, int ev) const noexcept override
{
return error.value() == ev &&
&error.category() == this;
}
};
inline
boost::system::error_category const&
get_parse_error_category()
{
static parse_error_category const cat{};
return cat;
}
inline
boost::system::error_code
make_error_code(parse_error ev)
{
return error_code(static_cast<int>(ev),
get_parse_error_category());
}
} // http
} // beast
namespace boost {
namespace system {
template<>
struct is_error_code_enum<beast::http::parse_error>
{
static bool const value = true;
};
} // system
} // boost
#include <beast/http/impl/parse_error.ipp>
#endif

View File

@ -44,7 +44,7 @@ namespace http {
@param parser An object meeting the requirements of Parser
which will receive the data.
@throws boost::system::system_error on failure.
@throws system_error Thrown on failure.
*/
template<class SyncReadStream, class DynamicBuffer, class Parser>
void
@ -165,7 +165,7 @@ async_parse(AsyncReadStream& stream, DynamicBuffer& dynabuf,
@param msg An object used to store the message. Any
contents will be overwritten.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
template<class SyncReadStream, class DynamicBuffer,
bool isRequest, class Body, class Headers>

View File

@ -40,7 +40,7 @@ namespace http {
@param msg The message to write.
@throws boost::system::error Thrown on failure.
@throws system_error Thrown on failure.
*/
template<class SyncWriteStream,
bool isRequest, class Body, class Headers>

View File

@ -1,92 +0,0 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_WEBSOCKET_DETAIL_ERROR_HPP
#define BEAST_WEBSOCKET_DETAIL_ERROR_HPP
#include <beast/websocket/error.hpp>
namespace boost {
namespace system {
template<>
struct is_error_code_enum<beast::websocket::error>
{
static bool const value = true;
};
} // system
} // boost
namespace beast {
namespace websocket {
namespace detail {
class error_category : public boost::system::error_category
{
public:
const char*
name() const noexcept override
{
return "websocket";
}
std::string
message(int ev) const override
{
switch(static_cast<error>(ev))
{
case error::closed: return "WebSocket connection closed normally";
case error::failed: return "WebSocket connection failed due to a protocol violation";
case error::handshake_failed: return "WebSocket Upgrade handshake failed";
case error::keep_alive: return "WebSocket Upgrade handshake failed but connection is still open";
case error::response_malformed: return "malformed HTTP response";
case error::response_failed: return "upgrade request failed";
case error::response_denied: return "upgrade request denied";
case error::request_malformed: return "malformed HTTP request";
case error::request_invalid: return "upgrade request invalid";
case error::request_denied: return "upgrade request denied";
default:
return "websocket error";
}
}
boost::system::error_condition
default_error_condition(int ev) const noexcept override
{
return boost::system::error_condition(ev, *this);
}
bool
equivalent(int ev,
boost::system::error_condition const& condition
) const noexcept override
{
return condition.value() == ev &&
&condition.category() == this;
}
bool
equivalent(error_code const& error, int ev) const noexcept override
{
return error.value() == ev &&
&error.category() == this;
}
};
inline
boost::system::error_category const&
get_error_category()
{
static detail::error_category const cat{};
return cat;
}
} // detail
} // websocket
} // beast
#endif

View File

@ -16,10 +16,8 @@ namespace websocket {
/// Error codes returned from @ref stream operations.
enum class error
{
success = 0,
/// Both sides performed a WebSocket close
closed,
closed = 1,
/// WebSocket connection failed, protocol violation
failed,
@ -52,11 +50,6 @@ enum class error
general
};
#if ! GENERATING_DOCS
error_code
make_error_code(error e);
#endif
} // websocket
} // beast

View File

@ -5,20 +5,93 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_WEBSOCKET_IMPL_ERROR_IPP_HPP
#define BEAST_WEBSOCKET_IMPL_ERROR_IPP_HPP
#ifndef BEAST_WEBSOCKET_IMPL_ERROR_IPP
#define BEAST_WEBSOCKET_IMPL_ERROR_IPP
#include <beast/websocket/detail/error.hpp>
namespace boost {
namespace system {
template<>
struct is_error_code_enum<beast::websocket::error>
{
static bool const value = true;
};
} // system
} // boost
namespace beast {
namespace websocket {
namespace detail {
class websocket_error_category : public error_category
{
public:
const char*
name() const noexcept override
{
return "websocket";
}
std::string
message(int ev) const override
{
switch(static_cast<error>(ev))
{
case error::closed: return "WebSocket connection closed normally";
case error::failed: return "WebSocket connection failed due to a protocol violation";
case error::handshake_failed: return "WebSocket Upgrade handshake failed";
case error::keep_alive: return "WebSocket Upgrade handshake failed but connection is still open";
case error::response_malformed: return "malformed HTTP response";
case error::response_failed: return "upgrade request failed";
case error::response_denied: return "upgrade request denied";
case error::request_malformed: return "malformed HTTP request";
case error::request_invalid: return "upgrade request invalid";
case error::request_denied: return "upgrade request denied";
default:
return "websocket error";
}
}
error_condition
default_error_condition(int ev) const noexcept override
{
return error_condition(ev, *this);
}
bool
equivalent(int ev,
error_condition const& condition
) const noexcept override
{
return condition.value() == ev &&
&condition.category() == this;
}
bool
equivalent(error_code const& error, int ev) const noexcept override
{
return error.value() == ev &&
&error.category() == this;
}
};
inline
error_category const&
get_error_category()
{
static detail::websocket_error_category const cat{};
return cat;
}
} // detail
inline
error_code
make_error_code(error e)
{
return error_code(
static_cast<int>(e), detail::get_error_category());
static_cast<std::underlying_type<error>::type>(e),
detail::get_error_category());
}
} // websocket

View File

@ -352,7 +352,7 @@ public:
HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
void
accept();
@ -462,7 +462,7 @@ public:
then to received WebSocket frames. The implementation will
copy the caller provided data before the function returns.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
template<class ConstBufferSequence>
void
@ -585,7 +585,7 @@ public:
Ownership is not transferred, the implementation will not access
this object from other threads.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
// VFALCO TODO This should also take a DynamicBuffer with any leftover bytes.
template<class Body, class Headers>
@ -700,7 +700,7 @@ public:
@param resource The requesting URI, which may not be empty,
required by the HTTP protocol.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
@par Example
@code
@ -837,7 +837,7 @@ public:
@param cr The reason for the close.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
void
close(close_reason const& cr);
@ -937,7 +937,7 @@ public:
@param payload The payload of the ping message, which may be empty.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
void
ping(ping_data const& payload);
@ -1031,7 +1031,7 @@ public:
@param dynabuf A dynamic buffer to hold the message data after
any masking or decompression has been applied.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
template<class DynamicBuffer>
void
@ -1166,7 +1166,7 @@ public:
@param dynabuf A dynamic buffer to hold the message data after
any masking or decompression has been applied.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
template<class DynamicBuffer>
void
@ -1304,7 +1304,7 @@ public:
the memory locations pointed to by buffers remains valid
until the completion handler is called.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
@note This function always sends an entire message. To
send a message in fragments, use @ref write_frame.
@ -1341,7 +1341,7 @@ public:
@param ec Set to indicate what error occurred, if any.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
@note This function always sends an entire message. To
send a message in fragments, use @ref write_frame.
@ -1426,7 +1426,7 @@ public:
@param buffers One or more buffers containing the frame's
payload data.
@throws boost::system::system_error Thrown on failure.
@throws system_error Thrown on failure.
*/
template<class ConstBufferSequence>
void
@ -1500,7 +1500,7 @@ public:
Copies will be made of the handler as required. The equivalent
function signature of the handler must be:
@code void handler(
boost::system::error_code const& error // result of operation
error_code const& error // result of operation
); @endcode
*/
template<class ConstBufferSequence, class WriteHandler>

View File

@ -59,7 +59,7 @@ public:
decltype(fs)&, streambuf> srs(fs);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
boost::system::error_code ec;
error_code ec;
boost::asio::read(srs, buffer(&s[0], s.size()), ec);
if(! ec)
{
@ -78,7 +78,7 @@ public:
srs.capacity(3);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
boost::system::error_code ec;
error_code ec;
boost::asio::read(srs, buffer(&s[0], s.size()), ec);
if(! ec)
{
@ -96,7 +96,7 @@ public:
decltype(fs)&, streambuf> srs(fs);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
boost::system::error_code ec;
error_code ec;
boost::asio::async_read(
srs, buffer(&s[0], s.size()), do_yield[ec]);
if(! ec)
@ -116,7 +116,7 @@ public:
srs.capacity(3);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
boost::system::error_code ec;
error_code ec;
boost::asio::async_read(
srs, buffer(&s[0], s.size()), do_yield[ec]);
if(! ec)

View File

@ -209,19 +209,19 @@ public:
p.on_body_rv(onBodyRv);
error_code ec;
p.write(buffer(s1.data(), s1.size()), ec);
if(ec == test::fail_error)
if(ec == test::error::fail_error)
continue;
if(! BEAST_EXPECT(! ec))
break;
if(! BEAST_EXPECT(s2.empty() || ! p.complete()))
break;
p.write(buffer(s2.data(), s2.size()), ec);
if(ec == test::fail_error)
if(ec == test::error::fail_error)
continue;
if(! BEAST_EXPECT(! ec))
break;
p.write_eof(ec);
if(ec == test::fail_error)
if(ec == test::error::fail_error)
continue;
if(! BEAST_EXPECT(! ec))
break;
@ -257,7 +257,7 @@ public:
p.on_body_rv(onBodyRv);
error_code ec;
p.write(buffer(s1.data(), s1.size()), ec);
if(ec == test::fail_error)
if(ec == test::error::fail_error)
continue;
if(ec)
{
@ -269,7 +269,7 @@ public:
if(! s2.empty())
{
p.write(buffer(s2.data(), s2.size()), ec);
if(ec == test::fail_error)
if(ec == test::error::fail_error)
continue;
if(ec)
{
@ -280,7 +280,7 @@ public:
break;
}
p.write_eof(ec);
if(ec == test::fail_error)
if(ec == test::error::fail_error)
continue;
BEAST_EXPECT(! p.complete());
BEAST_EXPECT((ec && ! ev) || ec == ev);

View File

@ -174,7 +174,7 @@ public:
error_code ec;
auto const used = write(data, size, ec);
if(ec)
throw boost::system::system_error{ec};
throw system_error{ec};
return used;
}
@ -189,7 +189,7 @@ public:
error_code ec;
auto const used = write(buffers, ec);
if(ec)
throw boost::system::system_error{ec};
throw system_error{ec};
return used;
}
@ -204,7 +204,7 @@ public:
error_code ec;
write_eof(ec);
if(ec)
throw boost::system::system_error{ec};
throw system_error{ec};
}
void

View File

@ -23,11 +23,13 @@ public:
BEAST_EXPECT(std::string{ec.category().name()} == name);
BEAST_EXPECT(! ec.message().empty());
BEAST_EXPECT(std::addressof(ec.category()) ==
std::addressof(get_parse_error_category()));
BEAST_EXPECT(get_parse_error_category().equivalent(static_cast<int>(ev),
ec.category().default_error_condition(static_cast<int>(ev))));
BEAST_EXPECT(get_parse_error_category().equivalent(
ec, static_cast<int>(ev)));
std::addressof(detail::get_parse_error_category()));
BEAST_EXPECT(detail::get_parse_error_category().equivalent(
static_cast<std::underlying_type<parse_error>::type>(ev),
ec.category().default_error_condition(
static_cast<std::underlying_type<parse_error>::type>(ev))));
BEAST_EXPECT(detail::get_parse_error_category().equivalent(
ec, static_cast<std::underlying_type<parse_error>::type>(ev)));
}
void run() override
@ -37,7 +39,6 @@ public:
check("http", parse_error::bad_uri);
check("http", parse_error::bad_version);
check("http", parse_error::bad_crlf);
check("http", parse_error::bad_request);
check("http", parse_error::bad_status);
check("http", parse_error::bad_reason);
check("http", parse_error::bad_field);

View File

@ -24,10 +24,12 @@ public:
BEAST_EXPECT(! ec.message().empty());
BEAST_EXPECT(std::addressof(ec.category()) ==
std::addressof(detail::get_error_category()));
BEAST_EXPECT(detail::get_error_category().equivalent(static_cast<int>(ev),
ec.category().default_error_condition(static_cast<int>(ev))));
BEAST_EXPECT(detail::get_error_category().equivalent(
ec, static_cast<int>(ev)));
static_cast<std::underlying_type<error>::type>(ev),
ec.category().default_error_condition(
static_cast<std::underlying_type<error>::type>(ev))));
BEAST_EXPECT(detail::get_error_category().equivalent(
ec, static_cast<std::underlying_type<error>::type>(ev)));
}
void run() override

View File

@ -816,7 +816,7 @@ public:
fail();
return false;
}
catch(boost::system::system_error const& se)
catch(system_error const& se)
{
if(se.code() != ev)
throw;