3
3
// /
4
4
5
5
#include " HTTP.h"
6
+ #include " InfluxDBException.h"
6
7
#include < iostream>
7
8
8
9
namespace influxdb
@@ -20,11 +21,14 @@ void HTTP::initCurl(const std::string& url)
20
21
{
21
22
CURLcode globalInitResult = curl_global_init (CURL_GLOBAL_ALL);
22
23
if (globalInitResult != CURLE_OK) {
23
- throw std::runtime_error ( std::string ( " cURL init " ) + curl_easy_strerror (globalInitResult));
24
+ throw InfluxDBException ( " HTTP::initCurl " , curl_easy_strerror (globalInitResult));
24
25
}
25
26
26
27
std::string writeUrl = url;
27
28
auto position = writeUrl.find (" ?" );
29
+ if (position == std::string::npos) {
30
+ throw InfluxDBException (" HTTP::initCurl" , " Database not specified" );
31
+ }
28
32
if (writeUrl.at (position - 1 ) != ' /' ) {
29
33
writeUrl.insert (position, " /write" );
30
34
} else {
@@ -71,7 +75,7 @@ std::string HTTP::query(const std::string& query)
71
75
curl_easy_setopt (readHandle, CURLOPT_WRITEDATA, &buffer);
72
76
response = curl_easy_perform (readHandle);
73
77
if (response != CURLE_OK) {
74
- throw std::runtime_error ( curl_easy_strerror (response));
78
+ throw InfluxDBException ( " HTTP::query " , curl_easy_strerror (response));
75
79
}
76
80
return buffer;
77
81
}
@@ -106,10 +110,10 @@ void HTTP::send(std::string&& post)
106
110
response = curl_easy_perform (writeHandle);
107
111
curl_easy_getinfo (writeHandle, CURLINFO_RESPONSE_CODE, &responseCode);
108
112
if (response != CURLE_OK) {
109
- throw std::runtime_error ( curl_easy_strerror (response));
113
+ throw InfluxDBException ( " HTTP::send " , curl_easy_strerror (response));
110
114
}
111
115
if (responseCode < 200 || responseCode > 206 ) {
112
- throw std::runtime_error ( " Response code : " + std::to_string (responseCode));
116
+ throw InfluxDBException ( " HTTP::send " , " Response code: " + std::to_string (responseCode));
113
117
}
114
118
}
115
119
0 commit comments