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

Commit 07a2ab1

Browse files
authored
Handle 400 status code when querying (#61)
1 parent 0bb9a05 commit 07a2ab1

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/HTTP.cxx

+5
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,20 @@ void HTTP::initCurlRead(const std::string& url)
6868
std::string HTTP::query(const std::string& query)
6969
{
7070
CURLcode response;
71+
long responseCode;
7172
std::string buffer;
7273
char* encodedQuery = curl_easy_escape(readHandle, query.c_str(), query.size());
7374
auto fullUrl = mReadUrl + std::string(encodedQuery);
7475
curl_easy_setopt(readHandle, CURLOPT_URL, fullUrl.c_str());
7576
curl_easy_setopt(readHandle, CURLOPT_WRITEDATA, &buffer);
7677
response = curl_easy_perform(readHandle);
78+
curl_easy_getinfo(readHandle, CURLINFO_RESPONSE_CODE, &responseCode);
7779
if (response != CURLE_OK) {
7880
throw InfluxDBException("HTTP::query", curl_easy_strerror(response));
7981
}
82+
if (responseCode != 200) {
83+
throw InfluxDBException("HTTP::query", "Status code: " + std::to_string(responseCode));
84+
}
8085
return buffer;
8186
}
8287

test/testQuery.cxx

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <boost/test/unit_test.hpp>
44

55
#include "../include/InfluxDBFactory.h"
6+
#include "../src/InfluxDBException.h"
67

78
namespace influxdb {
89
namespace test {
@@ -32,5 +33,11 @@ BOOST_AUTO_TEST_CASE(failedQuery1)
3233
BOOST_CHECK_EQUAL(points.size(), 0);
3334
}
3435

36+
BOOST_AUTO_TEST_CASE(failedQuery2)
37+
{
38+
auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test");
39+
BOOST_CHECK_THROW(influxdb->query("SELECT *from test1 WHEREhost = 'localhost' LIMIT 3"), InfluxDBException);
40+
}
41+
3542
} // namespace test
3643
} // namespace influxdb

0 commit comments

Comments
 (0)