Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Handle 400 status code when querying (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Mar 21, 2020
1 parent 0bb9a05 commit 07a2ab1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HTTP.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ void HTTP::initCurlRead(const std::string& url)
std::string HTTP::query(const std::string& query)
{
CURLcode response;
long responseCode;
std::string buffer;
char* encodedQuery = curl_easy_escape(readHandle, query.c_str(), query.size());
auto fullUrl = mReadUrl + std::string(encodedQuery);
curl_easy_setopt(readHandle, CURLOPT_URL, fullUrl.c_str());
curl_easy_setopt(readHandle, CURLOPT_WRITEDATA, &buffer);
response = curl_easy_perform(readHandle);
curl_easy_getinfo(readHandle, CURLINFO_RESPONSE_CODE, &responseCode);
if (response != CURLE_OK) {
throw InfluxDBException("HTTP::query", curl_easy_strerror(response));
}
if (responseCode != 200) {
throw InfluxDBException("HTTP::query", "Status code: " + std::to_string(responseCode));
}
return buffer;
}

Expand Down
7 changes: 7 additions & 0 deletions test/testQuery.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <boost/test/unit_test.hpp>

#include "../include/InfluxDBFactory.h"
#include "../src/InfluxDBException.h"

namespace influxdb {
namespace test {
Expand Down Expand Up @@ -32,5 +33,11 @@ BOOST_AUTO_TEST_CASE(failedQuery1)
BOOST_CHECK_EQUAL(points.size(), 0);
}

BOOST_AUTO_TEST_CASE(failedQuery2)
{
auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test");
BOOST_CHECK_THROW(influxdb->query("SELECT *from test1 WHEREhost = 'localhost' LIMIT 3"), InfluxDBException);
}

} // namespace test
} // namespace influxdb

0 comments on commit 07a2ab1

Please sign in to comment.