Skip to content

Commit

Permalink
Tidy up error types:
Browse files Browse the repository at this point in the history
* Restructure header material
* Clean up namespace type injections
* Remove extraneous 'success' constants
  • Loading branch information
vinniefalco committed Oct 4, 2016
1 parent 0bdd78e commit 744dcaf
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 275 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add Secure WebSocket example
* Fix message_v1 constructor
* Tidy up DynamicBuffer requirements
* Tidy up error types and headers

--------------------------------------------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions doc/quickref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion examples/http_crawl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 1 addition & 3 deletions extras/beast/test/fail_counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ namespace test {

enum error
{
success = 0,

fail_error
fail_error = 1
};

namespace detail {
Expand Down
4 changes: 2 additions & 2 deletions include/beast/core/async_completion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
9 changes: 2 additions & 7 deletions include/beast/core/detail/stream_concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
Expand Down Expand Up @@ -79,9 +80,6 @@ class is_AsyncWriteStream
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>())),
Expand All @@ -108,9 +106,6 @@ class is_SyncReadStream
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>())),
Expand Down
12 changes: 12 additions & 0 deletions include/beast/core/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
107 changes: 107 additions & 0 deletions include/beast/http/impl/parse_error.ipp
Original file line number Diff line number Diff line change
@@ -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_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";
}
}

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<int>(ev),
detail::get_parse_error_category()};
}

} // http
} // beast

#endif
2 changes: 1 addition & 1 deletion include/beast/http/impl/read.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 3 additions & 3 deletions include/beast/http/impl/write.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
129 changes: 2 additions & 127 deletions include/beast/http/parse_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ namespace http {

enum class parse_error
{
success = 0,

connection_closed,
connection_closed = 1,

bad_method,
bad_uri,
Expand Down Expand Up @@ -45,132 +43,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
Loading

0 comments on commit 744dcaf

Please sign in to comment.