diff options
author | Dmitry Mikushin <dmitry@kernelgen.org> | 2022-09-08 21:18:27 +0200 |
---|---|---|
committer | Dmitry Mikushin <dmitry@kernelgen.org> | 2022-09-08 21:18:27 +0200 |
commit | bf0905d2f9d373b4b05e270f8145344d7571a36b (patch) | |
tree | 3189b18bdeebda0cbb9a592a1fd6542f13e4b5a8 /src | |
parent | ce50cefc0a7b9aea99344a4b0a418544e2deca4a (diff) |
If the request did not complete correctly, show the error information. If no detailed error information was written to errbuf, show the more generic information from curl_easy_strerror instead
Diffstat (limited to 'src')
-rw-r--r-- | src/net/CurlHttpClient.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/net/CurlHttpClient.cpp b/src/net/CurlHttpClient.cpp index ff06138..2a397f9 100644 --- a/src/net/CurlHttpClient.cpp +++ b/src/net/CurlHttpClient.cpp @@ -57,13 +57,27 @@ std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpRe curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteString); + char errbuf[CURL_ERROR_SIZE] {}; + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); + auto res = curl_easy_perform(curl); curl_slist_free_all(headers); curl_easy_cleanup(curl); curl_mime_free(mime); + // If the request did not complete correctly, show the error + // information. If no detailed error information was written to errbuf + // show the more generic information from curl_easy_strerror instead. if (res != CURLE_OK) { - throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); + size_t len = strlen(errbuf); + std::string errmsg; + if (len) { + errmsg = std::string(errbuf) + ((errbuf[len - 1] != '\n') ? "\n" : ""); + } + else { + errmsg = curl_easy_strerror(res); + } + throw std::runtime_error(std::string("curl error: ") + errmsg); } return _httpParser.extractBody(response); |