# HG changeset patch # User František Kučera # Date 1647384182 -3600 # Node ID 6b913e82f52afca9ff8e2bf5df9931f4095c03b6 # Parent 479557122717d1e66d9c4228bb8a5225db3ae8c3 provide also error details in exception and output diff -r 479557122717 -r 6b913e82f52a src/HTTPClient.cpp --- a/src/HTTPClient.cpp Tue Mar 15 23:29:36 2022 +0100 +++ b/src/HTTPClient.cpp Tue Mar 15 23:43:02 2022 +0100 @@ -30,6 +30,7 @@ class HTTPClient::HTTPClientImpl { public: CURL* curl; + char curlErrorBuffer[CURL_ERROR_SIZE]; std::stringstream responseBody; std::stringstream responseHeaders; @@ -109,6 +110,9 @@ return r; }); + // set the error buffer + curl_easy_setopt(impl->curl, CURLOPT_ERRORBUFFER, impl->curlErrorBuffer); + return new HTTPClient(impl); } @@ -130,6 +134,9 @@ for (size_t i = 0; i < request.headers.size(); i += 2) requestHeders.append(request.headers[i] + ": " + request.headers[i + 1]); // TODO: validate, no CR/LF... curl_easy_setopt(impl->curl, CURLOPT_HTTPHEADER, requestHeders.getList()); + // clear the error buffer + impl->curlErrorBuffer[0] = 0; + // do HTTP call CURLcode result = curl_easy_perform(impl->curl); @@ -143,7 +150,7 @@ return response; } else { - throw Exception(curl_easy_strerror(result)); + throw Exception(curl_easy_strerror(result), impl->curlErrorBuffer); } } diff -r 479557122717 -r 6b913e82f52a src/HTTPClient.h --- a/src/HTTPClient.h Tue Mar 15 23:29:36 2022 +0100 +++ b/src/HTTPClient.h Tue Mar 15 23:43:02 2022 +0100 @@ -68,14 +68,26 @@ }; class Exception : public std::runtime_error { + private: + std::string details; public: + Exception(const std::string& message, const std::string& details) : runtime_error(message), details(details) { + } + Exception(const std::string& message) : runtime_error(message) { } Exception(const char* message) : runtime_error(message) { } + std::string getDetails() const { + return details; + } + + std::string getFullMessage() const { + return std::string(what()) + ": " + details; + } }; virtual ~HTTPClient(); diff -r 479557122717 -r 6b913e82f52a src/HTTPHandler.h --- a/src/HTTPHandler.h Tue Mar 15 23:29:36 2022 +0100 +++ b/src/HTTPHandler.h Tue Mar 15 23:43:02 2022 +0100 @@ -162,7 +162,7 @@ responseHeaders.push_back(convertor.from_bytes(response.headers[i + 1])); } } catch (const HTTPClient::Exception& e) { - body = e.what(); + body = e.getFullMessage(); // TODO: move error message into separate attribute? }