diff --git a/src/core/curl_exception.cpp b/src/core/curl_exception.cpp index e39931e..b824049 100644 --- a/src/core/curl_exception.cpp +++ b/src/core/curl_exception.cpp @@ -19,12 +19,16 @@ #include "curl_exception.h" using namespace bot::exception; -curl_exception::curl_exception(const std::string &message, CURLcode error_code) : m_message(message), m_error_code(error_code) { +curl_exception::curl_exception(const std::string &message, CURLcode error) : m_message(message), m_error(error) { } curl_exception::~curl_exception() noexcept { } -const char* curl_exception::what() const noexcept { - return m_message.c_str(); +CURLcode curl_exception::error() const noexcept { + return m_error; +} + +const char* curl_exception::what() const noexcept { + return !m_message.empty() ? m_message.c_str() : curl_easy_strerror(m_error); } diff --git a/src/core/curl_exception.h b/src/core/curl_exception.h index 0fddb24..ddc1187 100644 --- a/src/core/curl_exception.h +++ b/src/core/curl_exception.h @@ -27,13 +27,14 @@ namespace bot { namespace exception { class curl_exception : public std::exception { public: - explicit curl_exception(const std::string &message, CURLcode error_code); + explicit curl_exception(const std::string &message, CURLcode error); virtual ~curl_exception() noexcept; + CURLcode error() const noexcept; const char* what() const noexcept override; private: - const std::string m_message; - const CURLcode m_error_code; + std::string m_message; + CURLcode m_error; }; } }; diff --git a/src/core/http_request.cpp b/src/core/http_request.cpp index 400206c..054a241 100644 --- a/src/core/http_request.cpp +++ b/src/core/http_request.cpp @@ -40,7 +40,7 @@ const http_response http_request::get(const std::string &url, const http_headers curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status); curl_easy_reset(instance); return result == CURLE_OK ? - response : throw bot::exception::curl_exception(strlen(error) ? error : curl_easy_strerror(result), result); + response : throw bot::exception::curl_exception(error, result); } const http_response http_request::post(const std::string &url, const std::string &content, const http_headers &headers) { @@ -58,7 +58,7 @@ const http_response http_request::post(const std::string &url, const std::string curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status); curl_easy_reset(instance); return result == CURLE_OK ? - response : throw bot::exception::curl_exception(strlen(error) ? error : curl_easy_strerror(result), result); + response : throw bot::exception::curl_exception(error, result); } std::string http_request::legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls) {