diff options
author | Oleg Morozenkov <m@oleg.rocks> | 2022-09-09 03:50:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-09 03:50:12 +0300 |
commit | c82fa054194126e400e7e87dabbdaf20d847703c (patch) | |
tree | 19c519f0180fd395a87aa59cf6382c3c6a8dc320 /src | |
parent | b3559b2bb2604adf31f8f370474819b7f29ccb8b (diff) | |
parent | bf0905d2f9d373b4b05e270f8145344d7571a36b (diff) |
Merge pull request #225 from dmikushin/curl_error_message
Give more detailed error messages for CURL http request, when possible
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); |