diff --git a/include/cserver/clients/http/component.hpp b/include/cserver/clients/http/component.hpp index 17cf04a..79432dc 100644 --- a/include/cserver/clients/http/component.hpp +++ b/include/cserver/clients/http/component.hpp @@ -3,9 +3,9 @@ #include #include #include +#include #include #include -#include #include namespace cserver::server::http { @@ -46,12 +46,24 @@ struct HttpClient { boost::asio::ssl::stream socket(this->taskProcessor.ioContext, this->ctx); boost::asio::ip::tcp::resolver::iterator endpoint = co_await this->resolver.async_resolve({request.request.url.host(), request.request.url.has_port() ? request.request.url.port() : request.request.url.scheme()}, boost::asio::use_awaitable); - co_await boost::asio::async_connect(socket.lowest_layer(), endpoint, boost::asio::use_awaitable); - co_await socket.async_handshake(boost::asio::ssl::stream_base::client, boost::asio::use_awaitable); + auto [ec, _] = co_await boost::asio::async_connect(socket.lowest_layer(), endpoint, boost::asio::as_tuple(boost::asio::use_awaitable)); + if(ec) { + throw BoostErrorWrapper{ec}; + }; + co_await socket.async_handshake(boost::asio::ssl::stream_base::client, boost::asio::redirect_error(boost::asio::use_awaitable, ec)); + if(ec) { + throw BoostErrorWrapper{ec}; + }; std::string req(request.request.ToString()); - co_await boost::asio::async_write(socket, boost::asio::buffer(req.data(), req.size()), boost::asio::use_awaitable); + co_await boost::asio::async_write(socket, boost::asio::buffer(req.data(), req.size()), boost::asio::redirect_error(boost::asio::use_awaitable, ec)); + if(ec) { + throw BoostErrorWrapper{ec}; + }; std::string serverResponse; - co_await boost::asio::async_read_until(socket, boost::asio::dynamic_buffer(serverResponse), "\r\n\r\n", boost::asio::use_awaitable); + co_await boost::asio::async_read_until(socket, boost::asio::dynamic_buffer(serverResponse), "\r\n\r\n", boost::asio::redirect_error(boost::asio::use_awaitable, ec)); + if(ec) { + throw BoostErrorWrapper{ec}; + }; server::http::HTTPResponse response; std::istringstream responseStream(std::move(serverResponse)); std::string httpVersion; @@ -67,7 +79,10 @@ struct HttpClient { if(response.headers.contains("Content-Length")) { auto size = std::stoi(response.headers["Content-Length"]); response.body.reserve(size); - co_await boost::asio::async_read(socket, boost::asio::dynamic_buffer(response.body), boost::asio::transfer_at_least(size), boost::asio::use_awaitable); + co_await boost::asio::async_read(socket, boost::asio::dynamic_buffer(response.body), boost::asio::transfer_at_least(size), boost::asio::redirect_error(boost::asio::use_awaitable, ec)); + if(ec) { + throw BoostErrorWrapper{ec}; + }; co_return response; }; for(;;) { @@ -76,7 +91,7 @@ struct HttpClient { break; }; if(ec) { - throw ec; + throw BoostErrorWrapper{ec}; }; }; co_return response; diff --git a/include/cserver/utils/boost_error_wrapper.hpp b/include/cserver/utils/boost_error_wrapper.hpp new file mode 100644 index 0000000..8777262 --- /dev/null +++ b/include/cserver/utils/boost_error_wrapper.hpp @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace cserver { + +struct BoostErrorWrapper : std::exception, boost::system::error_code { + std::string errorMessage; + BoostErrorWrapper(boost::system::error_code ec) : errorMessage(ec.what()) {}; + auto what() const noexcept -> const char* override { + return this->errorMessage.c_str(); + }; +}; + +} // namespace cserver