Add core errors and conditions

This commit is contained in:
Vinnie Falco
2018-12-30 12:38:35 -08:00
parent 25f144f936
commit 2d3912751b
7 changed files with 246 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ Version 201
* Decay bound arguments in handler wrapper parameters * Decay bound arguments in handler wrapper parameters
* Add bind_back_handler * Add bind_back_handler
* Tidy up default-constructed iterators * Tidy up default-constructed iterators
* Add core errors and conditions
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -231,6 +231,8 @@
</simplelist> </simplelist>
<bridgehead renderas="sect3">Constants</bridgehead> <bridgehead renderas="sect3">Constants</bridgehead>
<simplelist type="vert" columns="1"> <simplelist type="vert" columns="1">
<member><link linkend="beast.ref.boost__beast__condition">condition</link></member>
<member><link linkend="beast.ref.boost__beast__error">error</link></member>
<member><link linkend="beast.ref.boost__beast__file_mode">file_mode</link></member> <member><link linkend="beast.ref.boost__beast__file_mode">file_mode</link></member>
</simplelist> </simplelist>
</entry> </entry>

View File

@@ -52,7 +52,34 @@ enum errc{};
namespace errc = boost::system::errc; namespace errc = boost::system::errc;
#endif #endif
//------------------------------------------------------------------------------
/// Error codes returned from library operations
enum class error
{
/** The socket was closed due to a timeout
This error indicates that a socket was closed due to a
a timeout detected during an operation.
Error codes with this value will compare equal to @ref condition::timeout.
*/
timeout = 1
};
/// Error conditions corresponding to sets of library error codes.
enum class condition
{
/** The operation timed out
This error indicates that an operation took took too long.
*/
timeout = 1
};
} // beast } // beast
} // boost } // boost
#include <boost/beast/core/impl/error.hpp>
#endif #endif

View File

@@ -0,0 +1,79 @@
//
// Copyright (c) 2016-2017 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_IMPL_ERROR_HPP
#define BOOST_BEAST_IMPL_ERROR_HPP
#include <type_traits>
namespace boost {
namespace system {
template<>
struct is_error_code_enum<beast::error>
{
static bool const value = true;
};
template<>
struct is_error_condition_enum<beast::condition>
{
static bool const value = true;
};
} // system
} // boost
namespace boost {
namespace beast {
namespace detail {
class error_codes : public error_category
{
public:
BOOST_BEAST_DECL
const char*
name() const noexcept override;
BOOST_BEAST_DECL
std::string
message(int ev) const override;
BOOST_BEAST_DECL
error_condition
default_error_condition(int ev) const noexcept override;
};
class error_conditions : public error_category
{
public:
BOOST_BEAST_DECL
const char*
name() const noexcept override;
BOOST_BEAST_DECL
std::string
message(int cv) const override;
};
} // detail
BOOST_BEAST_DECL
error_code
make_error_code(error e);
BOOST_BEAST_DECL
error_condition
make_error_condition(condition c);
} // beast
} // boost
#include <boost/beast/core/impl/error.ipp>
#endif

View File

@@ -0,0 +1,92 @@
//
// Copyright (c) 2016-2017 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_IMPL_ERROR_IPP
#define BOOST_BEAST_IMPL_ERROR_IPP
namespace boost {
namespace beast {
namespace detail {
const char*
error_codes::
name() const noexcept
{
return "boost.beast";
}
std::string
error_codes::
message(int ev) const
{
switch(static_cast<error>(ev))
{
default:
case error::timeout: return
"The socket was closed due to a timeout";
}
}
error_condition
error_codes::
default_error_condition(int ev) const noexcept
{
switch(static_cast<error>(ev))
{
default:
// return {ev, *this};
case error::timeout:
return condition::timeout;
}
}
//------------------------------------------------------------------------------
const char*
error_conditions::
name() const noexcept
{
return "boost.beast";
}
std::string
error_conditions::
message(int cv) const
{
switch(static_cast<condition>(cv))
{
default:
case condition::timeout:
return "The operation timed out";
}
}
} // detail
error_code
make_error_code(error e)
{
static detail::error_codes const cat{};
return error_code{static_cast<
std::underlying_type<error>::type>(e), cat};
}
error_condition
make_error_condition(condition c)
{
static detail::error_conditions const cat{};
return error_condition{static_cast<
std::underlying_type<condition>::type>(c), cat};
}
} // beast
} // boost
#endif

View File

@@ -9,3 +9,47 @@
// Test that header file is self-contained. // Test that header file is self-contained.
#include <boost/beast/core/error.hpp> #include <boost/beast/core/error.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <memory>
namespace boost {
namespace beast {
class error_test : public unit_test::suite
{
public:
// no condition
void check(error e)
{
auto const ec = make_error_code(e);
ec.category().name();
BEAST_EXPECT(! ec.message().empty());
}
void check(condition c, error e)
{
{
auto const ec = make_error_code(e);
BEAST_EXPECT(ec.category().name() != nullptr);
BEAST_EXPECT(! ec.message().empty());
BEAST_EXPECT(ec == c);
}
{
auto ec = make_error_condition(c);
BEAST_EXPECT(ec.category().name() != nullptr);
BEAST_EXPECT(! ec.message().empty());
BEAST_EXPECT(ec == c);
}
}
void run() override
{
check(condition::timeout, error::timeout);
}
};
BEAST_DEFINE_TESTSUITE(beast,websocket,error);
} // beast
} // boost

View File

@@ -102,7 +102,7 @@ void fxx() {
error_code ec; error_code ec;
request<string_body> req; request<string_body> req;
read(sock, buffer, req, ec); read(sock, buffer, req, ec);
if(ec == error::buffer_overflow) if(ec == http::error::buffer_overflow)
std::cerr << "Buffer limit exceeded!" << std::endl; std::cerr << "Buffer limit exceeded!" << std::endl;
//] //]