Merge branch 'exception_refactor' into develop

Conflicts:
	changelog.md
	websocketpp/impl/connection_impl.hpp
This commit is contained in:
Peter Thorson
2014-08-09 15:44:46 -05:00
10 changed files with 87 additions and 83 deletions

View File

@@ -538,7 +538,7 @@ public:
lib::error_code ec;
connection_ptr con = this->get_con_from_hdl(hdl,ec);
if (ec) {
throw ec;
throw exception(ec);
}
return con;
}

View File

@@ -217,21 +217,30 @@ namespace websocketpp {
class exception : public std::exception {
public:
exception(std::string const & msg, error::value p_code = error::general)
: m_msg(msg), m_code(p_code) {}
exception(std::string const & msg, lib::error_code ec = make_error_code(error::general))
: m_msg(msg), m_code(ec)
{}
explicit exception(lib::error_code ec)
: m_code(ec)
{}
~exception() throw() {}
virtual char const * what() const throw() {
return m_msg.c_str();
if (m_msg.empty()) {
return m_code.message().c_str();
} else {
return m_msg.c_str();
}
}
error::value code() const throw() {
lib::error_code code() const throw() {
return m_code;
}
std::string m_msg;
error::value m_code;
lib::error_code m_code;
};
} // namespace websocketpp

View File

@@ -205,17 +205,17 @@ void connection<config>::ping(const std::string& payload, lib::error_code& ec) {
}
template<typename config>
void connection<config>::ping(const std::string& payload) {
void connection<config>::ping(std::string const & payload) {
lib::error_code ec;
ping(payload,ec);
if (ec) {
throw ec;
throw exception(ec);
}
}
template<typename config>
void connection<config>::handle_pong_timeout(std::string payload, const lib::error_code &
ec)
void connection<config>::handle_pong_timeout(std::string payload,
lib::error_code const & ec)
{
if (ec) {
if (ec == transport::error::operation_aborted) {
@@ -270,11 +270,11 @@ void connection<config>::pong(const std::string& payload, lib::error_code& ec) {
}
template<typename config>
void connection<config>::pong(const std::string& payload) {
void connection<config>::pong(std::string const & payload) {
lib::error_code ec;
pong(payload,ec);
if (ec) {
throw ec;
throw exception(ec);
}
}
@@ -305,7 +305,7 @@ void connection<config>::close(close::status::value const code,
lib::error_code ec;
close(code,reason,ec);
if (ec) {
throw ec;
throw exception(ec);
}
}
@@ -431,7 +431,7 @@ connection<config>::get_requested_subprotocols() const {
}
template <typename config>
void connection<config>::add_subprotocol(const std::string & value,
void connection<config>::add_subprotocol(std::string const & value,
lib::error_code & ec)
{
if (m_is_server) {
@@ -451,17 +451,17 @@ void connection<config>::add_subprotocol(const std::string & value,
}
template <typename config>
void connection<config>::add_subprotocol(const std::string & value) {
void connection<config>::add_subprotocol(std::string const & value) {
lib::error_code ec;
this->add_subprotocol(value,ec);
if (ec) {
throw ec;
throw exception(ec);
}
}
template <typename config>
void connection<config>::select_subprotocol(const std::string & value,
void connection<config>::select_subprotocol(std::string const & value,
lib::error_code & ec)
{
if (!m_is_server) {
@@ -489,24 +489,24 @@ void connection<config>::select_subprotocol(const std::string & value,
}
template <typename config>
void connection<config>::select_subprotocol(const std::string & value) {
void connection<config>::select_subprotocol(std::string const & value) {
lib::error_code ec;
this->select_subprotocol(value,ec);
if (ec) {
throw ec;
throw exception(ec);
}
}
template <typename config>
const std::string &
connection<config>::get_request_header(const std::string &key) {
connection<config>::get_request_header(std::string const & key) {
return m_request.get_header(key);
}
template <typename config>
const std::string &
connection<config>::get_response_header(const std::string &key) {
connection<config>::get_response_header(std::string const & key) {
return m_response.get_header(key);
}
@@ -516,9 +516,8 @@ void connection<config>::set_status(http::status_code::value code)
//scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw error::make_error_code(error::invalid_state);
//throw exception("Call to set_status from invalid state",
// error::INVALID_STATE);
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}
m_response.set_status(code);
@@ -530,9 +529,8 @@ void connection<config>::set_status(http::status_code::value code,
//scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw error::make_error_code(error::invalid_state);
//throw exception("Call to set_status from invalid state",
// error::INVALID_STATE);
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}
m_response.set_status(code,msg);
@@ -542,9 +540,8 @@ void connection<config>::set_body(std::string const & value) {
//scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw error::make_error_code(error::invalid_state);
//throw exception("Call to set_status from invalid state",
// error::INVALID_STATE);
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}
m_response.set_body(value);
@@ -561,14 +558,16 @@ void connection<config>::append_header(std::string const & key,
// we are setting response headers for an incoming server connection
m_response.append_header(key,val);
} else {
throw error::make_error_code(error::invalid_state);
throw exception("Call to append_header from invalid state",
error::make_error_code(error::invalid_state));
}
} else {
if (m_internal_state == istate::USER_INIT) {
// we are setting initial headers for an outgoing client connection
m_request.append_header(key,val);
} else {
throw error::make_error_code(error::invalid_state);
throw exception("Call to append_header from invalid state",
error::make_error_code(error::invalid_state));
}
}
}
@@ -583,14 +582,16 @@ void connection<config>::replace_header(std::string const & key,
// we are setting response headers for an incoming server connection
m_response.replace_header(key,val);
} else {
throw error::make_error_code(error::invalid_state);
throw exception("Call to replace_header from invalid state",
error::make_error_code(error::invalid_state));
}
} else {
if (m_internal_state == istate::USER_INIT) {
// we are setting initial headers for an outgoing client connection
m_request.replace_header(key,val);
} else {
throw error::make_error_code(error::invalid_state);
throw exception("Call to replace_header from invalid state",
error::make_error_code(error::invalid_state));
}
}
}
@@ -604,14 +605,16 @@ void connection<config>::remove_header(std::string const & key)
// we are setting response headers for an incoming server connection
m_response.remove_header(key);
} else {
throw error::make_error_code(error::invalid_state);
throw exception("Call to remove_header from invalid state",
error::make_error_code(error::invalid_state));
}
} else {
if (m_internal_state == istate::USER_INIT) {
// we are setting initial headers for an outgoing client connection
m_request.remove_header(key);
} else {
throw error::make_error_code(error::invalid_state);
throw exception("Call to remove_header from invalid state",
error::make_error_code(error::invalid_state));
}
}
}
@@ -653,9 +656,8 @@ void connection<config>::handle_transport_init(lib::error_code const & ec) {
scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != istate::TRANSPORT_INIT) {
throw error::make_error_code(error::invalid_state);
//throw exception("handle_transport_init must be called from transport init state",
// error::INVALID_STATE);
throw exception("handle_transport_init must be called from transport init state",
error::make_error_code(error::invalid_state));
}
if (!ec) {
@@ -1697,8 +1699,7 @@ void connection<config>::atomic_state_change(istate_type req, istate_type dest,
scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != req) {
throw error::make_error_code(error::invalid_state);
//throw exception(msg,error::INVALID_STATE);
throw exception(msg,error::make_error_code(error::invalid_state));
}
m_internal_state = dest;
@@ -1712,8 +1713,7 @@ void connection<config>::atomic_state_change(istate_type internal_req,
scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != internal_req || m_state != external_req) {
throw error::make_error_code(error::invalid_state);
//throw exception(msg,error::INVALID_STATE);
throw exception(msg,error::make_error_code(error::invalid_state));
}
m_internal_state = internal_dest;
@@ -1726,8 +1726,7 @@ void connection<config>::atomic_state_check(istate_type req, std::string msg)
scoped_lock_type lock(m_connection_state_lock);
if (m_internal_state != req) {
throw error::make_error_code(error::invalid_state);
//throw exception(msg,error::INVALID_STATE);
throw exception(msg,error::make_error_code(error::invalid_state));
}
}

View File

@@ -104,7 +104,7 @@ template <typename connection, typename config>
void endpoint<connection,config>::interrupt(connection_hdl hdl) {
lib::error_code ec;
interrupt(hdl,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -120,7 +120,7 @@ template <typename connection, typename config>
void endpoint<connection,config>::pause_reading(connection_hdl hdl) {
lib::error_code ec;
pause_reading(hdl,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -136,7 +136,7 @@ template <typename connection, typename config>
void endpoint<connection,config>::resume_reading(connection_hdl hdl) {
lib::error_code ec;
resume_reading(hdl,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
@@ -157,7 +157,7 @@ void endpoint<connection,config>::send(connection_hdl hdl, std::string const & p
{
lib::error_code ec;
send(hdl,payload,op,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -175,7 +175,7 @@ void endpoint<connection,config>::send(connection_hdl hdl, void const * payload,
{
lib::error_code ec;
send(hdl,payload,len,op,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -191,7 +191,7 @@ template <typename connection, typename config>
void endpoint<connection,config>::send(connection_hdl hdl, message_ptr msg) {
lib::error_code ec;
send(hdl,msg,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -210,7 +210,7 @@ void endpoint<connection,config>::close(connection_hdl hdl, close::status::value
{
lib::error_code ec;
close(hdl,code,reason,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -227,7 +227,7 @@ void endpoint<connection,config>::ping(connection_hdl hdl, std::string const & p
{
lib::error_code ec;
ping(hdl,payload,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
template <typename connection, typename config>
@@ -244,7 +244,7 @@ void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & p
{
lib::error_code ec;
pong(hdl,payload,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
} // namespace websocketpp

View File

@@ -129,7 +129,7 @@ public:
lib::error_code ec;
start_accept(ec);
if (ec) {
throw ec;
throw exception(ec);
}
}

View File

@@ -165,7 +165,7 @@ public:
*
* @param ec A status value
*/
void set_proxy(const std::string & uri, lib::error_code & ec) {
void set_proxy(std::string const & uri, lib::error_code & ec) {
// TODO: return errors for illegal URIs here?
// TODO: should https urls be illegal for the moment?
m_proxy = uri;
@@ -174,10 +174,10 @@ public:
}
/// Set the proxy to connect through (exception)
void set_proxy(const std::string & uri) {
void set_proxy(std::string const & uri) {
lib::error_code ec;
set_proxy(uri,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
/// Set the basic auth credentials to use (exception free)
@@ -193,8 +193,8 @@ public:
*
* @param ec A status value
*/
void set_proxy_basic_auth(const std::string & username, const
std::string & password, lib::error_code & ec)
void set_proxy_basic_auth(std::string const & username, std::string const &
password, lib::error_code & ec)
{
if (!m_proxy_data) {
ec = make_error_code(websocketpp::error::invalid_state);
@@ -208,12 +208,12 @@ public:
}
/// Set the basic auth credentials to use (exception)
void set_proxy_basic_auth(const std::string & username, const
std::string & password)
void set_proxy_basic_auth(std::string const & username, std::string const &
password)
{
lib::error_code ec;
set_proxy_basic_auth(username,password,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
/// Set the proxy timeout duration (exception free)
@@ -240,7 +240,7 @@ public:
void set_proxy_timeout(long duration) {
lib::error_code ec;
set_proxy_timeout(duration,ec);
if (ec) { throw ec; }
if (ec) { throw exception(ec); }
}
const std::string & get_proxy() const {

View File

@@ -194,9 +194,7 @@ public:
void init_asio(io_service_ptr ptr) {
lib::error_code ec;
init_asio(ptr,ec);
if (ec) {
throw ec;
}
if (ec) { throw exception(ec); }
}
/// Initialize asio transport with internal io_service (exception free)
@@ -374,9 +372,7 @@ public:
void listen(boost::asio::ip::tcp::endpoint const & ep) {
lib::error_code ec;
listen(ep,ec);
if (ec) {
throw ec;
}
if (ec) { throw exception(ec); }
}
/// Set up endpoint for listening with protocol and port (exception free)
@@ -506,9 +502,7 @@ public:
{
lib::error_code ec;
listen(host,service,ec);
if (ec) {
throw ec;
}
if (ec) { throw exception(ec); }
}
/// Stop listening (exception free)
@@ -543,9 +537,7 @@ public:
void stop_listening() {
lib::error_code ec;
stop_listening(ec);
if (ec) {
throw ec;
}
if (ec) { throw exception(ec); }
}
/// Check if the endpoint is listening
@@ -729,9 +721,7 @@ public:
void async_accept(transport_con_ptr tcon, accept_handler callback) {
lib::error_code ec;
async_accept(tcon,callback,ec);
if (ec) {
throw ec;
}
if (ec) { throw exception(ec); }
}
protected:
/// Initialize logging