From 12b389a5790c484f98fbdb95fab6a8dccda5b46e Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Wed, 15 Jul 2020 10:27:38 -0700 Subject: [PATCH] Http/throw on read error (#274) * throw exception on Read from wire Error instead of returning -1 * throw exception if read from socket is not CURLE_OK * missing header for Win * fix header on windows * Add more description to transport errors --- sdk/core/azure-core/inc/http/http.hpp | 14 +++++--------- sdk/core/azure-core/src/http/curl/curl.cpp | 15 +++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/sdk/core/azure-core/inc/http/http.hpp b/sdk/core/azure-core/inc/http/http.hpp index a18b11bb5..07553a1a2 100644 --- a/sdk/core/azure-core/inc/http/http.hpp +++ b/sdk/core/azure-core/inc/http/http.hpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace Azure { namespace Core { namespace Http { @@ -263,20 +264,15 @@ namespace Azure { namespace Core { namespace Http { /* * Response exceptions */ - struct CouldNotResolveHostException : public std::exception + struct CouldNotResolveHostException : public std::runtime_error { - const char* what() const throw() { return "could not resolve host"; } - }; - - struct ErrorWhileWrittingResponse : public std::exception - { - const char* what() const throw() { return "could not write response"; } + explicit CouldNotResolveHostException(std::string const& msg) : std::runtime_error(msg) {} }; // Any other excpetion from transport layer without an specific exception defined above - struct TransportException : public std::exception + struct TransportException : public std::runtime_error { - const char* what() const throw() { return "Error on transport layer while sending request"; } + explicit TransportException(std::string const& msg) : std::runtime_error(msg) {} }; class Response { diff --git a/sdk/core/azure-core/src/http/curl/curl.cpp b/sdk/core/azure-core/src/http/curl/curl.cpp index ed72d62cd..f6f83ab2c 100644 --- a/sdk/core/azure-core/src/http/curl/curl.cpp +++ b/sdk/core/azure-core/src/http/curl/curl.cpp @@ -21,15 +21,13 @@ std::unique_ptr CurlTransport::Send(Context& context, Request& request { case CURLE_COULDNT_RESOLVE_HOST: { - throw Azure::Core::Http::CouldNotResolveHostException(); - } - case CURLE_WRITE_ERROR: - { - throw Azure::Core::Http::ErrorWhileWrittingResponse(); + throw new Azure::Core::Http::CouldNotResolveHostException( + "Could not resolve host " + request.GetHost()); } default: { - throw Azure::Core::Http::TransportException(); + throw new Azure::Core::Http::TransportException( + "Error while sending request. " + std::string(curl_easy_strerror(performing))); } } } @@ -526,14 +524,15 @@ int64_t CurlSession::ReadSocketToBuffer(uint8_t* buffer, int64_t bufferSize) if (!WaitForSocketReady(this->m_curlSocket, 0, 60000L)) { // TODO: Change this to somehing more relevant - throw; + throw new Azure::Core::Http::TransportException( + "Timeout waiting to read from Network socket"); } break; case CURLE_OK: break; default: // Error code while reading from socket - throw Azure::Core::Http::TransportException(); + throw new Azure::Core::Http::TransportException("Error while reading from network socket"); } } return readBytes;