From a63f7d8b7b9b0504acb298601b6e61beadab7299 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Thu, 6 Mar 2014 19:35:25 -0600 Subject: [PATCH] switch all exceptions to be of type websocketpp::exception --- test/roles/client.cpp | 6 +- test/roles/server.cpp | 2 +- websocketpp/endpoint.hpp | 2 +- websocketpp/impl/connection_impl.hpp | 80 +++++++++++------------ websocketpp/impl/endpoint_impl.hpp | 18 ++--- websocketpp/roles/server_endpoint.hpp | 2 +- websocketpp/transport/asio/connection.hpp | 18 ++--- websocketpp/transport/asio/endpoint.hpp | 20 ++---- 8 files changed, 69 insertions(+), 79 deletions(-) diff --git a/test/roles/client.cpp b/test/roles/client.cpp index 6948dc0b43..b435e4dbd1 100644 --- a/test/roles/client.cpp +++ b/test/roles/client.cpp @@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE( select_subprotocol ) { con->select_subprotocol("foo",ec); BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::server_only) ); - BOOST_CHECK_THROW( con->select_subprotocol("foo") , websocketpp::lib::error_code ); + BOOST_CHECK_THROW( con->select_subprotocol("foo") , websocketpp::exception ); } BOOST_AUTO_TEST_CASE( add_subprotocols_invalid ) { @@ -158,11 +158,11 @@ BOOST_AUTO_TEST_CASE( add_subprotocols_invalid ) { con->add_subprotocol("",ec); BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::invalid_subprotocol) ); - BOOST_CHECK_THROW( con->add_subprotocol("") , websocketpp::lib::error_code ); + BOOST_CHECK_THROW( con->add_subprotocol("") , websocketpp::exception ); con->add_subprotocol("foo,bar",ec); BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::invalid_subprotocol) ); - BOOST_CHECK_THROW( con->add_subprotocol("foo,bar") , websocketpp::lib::error_code ); + BOOST_CHECK_THROW( con->add_subprotocol("foo,bar") , websocketpp::exception ); } BOOST_AUTO_TEST_CASE( add_subprotocols ) { diff --git a/test/roles/server.cpp b/test/roles/server.cpp index ada03f1aed..5995dd3dc0 100644 --- a/test/roles/server.cpp +++ b/test/roles/server.cpp @@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE( accept_subprotocol_invalid ) { std::string o; - BOOST_CHECK_THROW(o = run_server_test(s,input), websocketpp::lib::error_code); + BOOST_CHECK_THROW(o = run_server_test(s,input), websocketpp::exception); } BOOST_AUTO_TEST_CASE( accept_subprotocol_two ) { diff --git a/websocketpp/endpoint.hpp b/websocketpp/endpoint.hpp index 6c08b131e8..a4ae51fb96 100644 --- a/websocketpp/endpoint.hpp +++ b/websocketpp/endpoint.hpp @@ -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; } diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 41260c6192..af4c74aef3 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -201,17 +201,17 @@ void connection::ping(const std::string& payload, lib::error_code& ec) { } template -void connection::ping(const std::string& payload) { +void connection::ping(std::string const & payload) { lib::error_code ec; ping(payload,ec); if (ec) { - throw ec; + throw exception(ec); } } template -void connection::handle_pong_timeout(std::string payload, const lib::error_code & - ec) +void connection::handle_pong_timeout(std::string payload, + lib::error_code const & ec) { if (ec) { if (ec == transport::error::operation_aborted) { @@ -229,7 +229,8 @@ void connection::handle_pong_timeout(std::string payload, const lib::err } template -void connection::pong(const std::string& payload, lib::error_code& ec) { +void connection::pong(std::string const & payload, lib::error_code & ec) +{ m_alog.write(log::alevel::devel,"connection pong"); if (m_state != session::state::open) { @@ -264,11 +265,11 @@ void connection::pong(const std::string& payload, lib::error_code& ec) { } template -void connection::pong(const std::string& payload) { +void connection::pong(std::string const & payload) { lib::error_code ec; pong(payload,ec); if (ec) { - throw ec; + throw exception(ec); } } @@ -297,7 +298,7 @@ void connection::close(close::status::value const code, lib::error_code ec; close(code,reason,ec); if (ec) { - throw ec; + throw exception(ec); } } @@ -423,7 +424,7 @@ connection::get_requested_subprotocols() const { } template -void connection::add_subprotocol(const std::string & value, +void connection::add_subprotocol(std::string const & value, lib::error_code & ec) { if (m_is_server) { @@ -443,17 +444,17 @@ void connection::add_subprotocol(const std::string & value, } template -void connection::add_subprotocol(const std::string & value) { +void connection::add_subprotocol(std::string const & value) { lib::error_code ec; this->add_subprotocol(value,ec); if (ec) { - throw ec; + throw exception(ec); } } template -void connection::select_subprotocol(const std::string & value, +void connection::select_subprotocol(std::string const & value, lib::error_code & ec) { if (!m_is_server) { @@ -481,24 +482,24 @@ void connection::select_subprotocol(const std::string & value, } template -void connection::select_subprotocol(const std::string & value) { +void connection::select_subprotocol(std::string const & value) { lib::error_code ec; this->select_subprotocol(value,ec); if (ec) { - throw ec; + throw exception(ec); } } template const std::string & -connection::get_request_header(const std::string &key) { +connection::get_request_header(std::string const & key) { return m_request.get_header(key); } template const std::string & -connection::get_response_header(const std::string &key) { +connection::get_response_header(std::string const & key) { return m_response.get_header(key); } @@ -508,9 +509,8 @@ void connection::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); @@ -522,9 +522,8 @@ void connection::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); @@ -534,9 +533,8 @@ void connection::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); @@ -553,14 +551,16 @@ void connection::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)); } } } @@ -575,14 +575,16 @@ void connection::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)); } } } @@ -596,14 +598,16 @@ void connection::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)); } } } @@ -645,9 +649,8 @@ void connection::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) { @@ -1654,8 +1657,7 @@ void connection::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; @@ -1669,8 +1671,7 @@ void connection::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; @@ -1683,8 +1684,7 @@ void connection::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)); } } diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index 2cd4ff2b29..06e695a9c7 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -104,7 +104,7 @@ template void endpoint::interrupt(connection_hdl hdl) { lib::error_code ec; interrupt(hdl,ec); - if (ec) { throw ec; } + if (ec) { throw exception(ec); } } template @@ -120,7 +120,7 @@ template void endpoint::pause_reading(connection_hdl hdl) { lib::error_code ec; pause_reading(hdl,ec); - if (ec) { throw ec; } + if (ec) { throw exception(ec); } } template @@ -136,7 +136,7 @@ template void endpoint::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::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 @@ -175,7 +175,7 @@ void endpoint::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 @@ -191,7 +191,7 @@ template void endpoint::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 @@ -210,7 +210,7 @@ void endpoint::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 @@ -227,7 +227,7 @@ void endpoint::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 @@ -244,7 +244,7 @@ void endpoint::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 diff --git a/websocketpp/roles/server_endpoint.hpp b/websocketpp/roles/server_endpoint.hpp index 88ceae7710..177fbcbc3d 100644 --- a/websocketpp/roles/server_endpoint.hpp +++ b/websocketpp/roles/server_endpoint.hpp @@ -129,7 +129,7 @@ public: lib::error_code ec; start_accept(ec); if (ec) { - throw ec; + throw exception(ec); } } diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index cd78fd2444..5cd63840a9 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -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 { diff --git a/websocketpp/transport/asio/endpoint.hpp b/websocketpp/transport/asio/endpoint.hpp index 21bb610e57..51b1f5edf3 100644 --- a/websocketpp/transport/asio/endpoint.hpp +++ b/websocketpp/transport/asio/endpoint.hpp @@ -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) @@ -365,9 +363,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) @@ -497,9 +493,7 @@ public: { lib::error_code ec; listen(host,service,ec); - if (ec) { - throw ec; - } + if (ec) { throw exception(ec); } } /// Stop listening (exception free) @@ -534,9 +528,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 @@ -720,9 +712,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