Improved error reporting

This commit is contained in:
Bob Polis 2023-11-05 17:19:04 +00:00
parent 5c53dc88e7
commit 5ceb6e1d6a

View File

@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
#include <sstream>
using namespace sc; using namespace sc;
@ -47,10 +48,18 @@ std::string requester::get(const std::string &url) {
curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str()); curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, this); curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, this);
auto success = curl_easy_perform(_h.get()); auto success = curl_easy_perform(_h.get());
if (success != CURLE_OK) throw std::runtime_error("could not get remote data"); if (success != CURLE_OK) {
std::ostringstream oss;
oss << "could not get remote data: " << curl_easy_strerror(success);
throw std::runtime_error {oss.str()};
}
long status; long status;
success = curl_easy_getinfo(_h.get(), CURLINFO_RESPONSE_CODE, &status); success = curl_easy_getinfo(_h.get(), CURLINFO_RESPONSE_CODE, &status);
if (success != CURLE_OK || status != 200) throw std::runtime_error("could not get remote data"); if (success != CURLE_OK || status != 200) {
std::ostringstream oss;
oss << "could not get remote data (" << status << ")";
throw std::runtime_error {oss.str()};
}
return _text; return _text;
} }
@ -69,6 +78,10 @@ std::string requester::post(const std::string& url, const std::string& json) {
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &response); curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &response);
auto success = curl_easy_perform(_h.get()); auto success = curl_easy_perform(_h.get());
if (success != CURLE_OK) throw std::runtime_error("could not post data"); if (success != CURLE_OK) {
std::ostringstream oss;
oss << "could not post data: " << curl_easy_strerror(success);
throw std::runtime_error {oss.str()};
}
return response; return response;
} }