Skip to content

Commit 18f797d

Browse files
committed
Replace deprecated exception types (#159)
1 parent 5df70f5 commit 18f797d

14 files changed

+42
-60
lines changed

include/InfluxDBException.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,27 @@ namespace influxdb
5656
/// \deprecated Use InfluxDBException instead - will be removed in v0.8.0
5757
class [[deprecated("Use InfluxDBException instead - will be removed in v0.8.0")]] NonExistentDatabase : public InfluxDBException{
5858
public :
59-
NonExistentDatabase(const std::string& source, const std::string& message) : InfluxDBException(source, message){}
59+
NonExistentDatabase(const std::string& source, const std::string& message) : InfluxDBException("influx-cxx [" + source + "]: " + message){}
6060
};
6161

6262
/// \deprecated Use InfluxDBException instead - will be removed in v0.8.0
6363
class [[deprecated("Use InfluxDBException instead - will be removed in v0.8.0")]] BadRequest : public InfluxDBException{
6464
public :
65-
BadRequest(const std::string& source, const std::string& message) : InfluxDBException(source, message){}
65+
BadRequest(const std::string& source, const std::string& message) : InfluxDBException("influx-cxx [" + source + "]: " + message){}
6666
};
6767

6868
/// \deprecated Use InfluxDBException instead - will be removed in v0.8.0
6969
class [[deprecated("Use InfluxDBException instead - will be removed in v0.8.0")]] ServerError : public InfluxDBException{
7070
public :
71-
ServerError(const std::string& source, const std::string& message) : InfluxDBException(source, message){}
71+
ServerError(const std::string& source, const std::string& message) : InfluxDBException("influx-cxx [" + source + "]: " + message){}
7272
};
7373

7474
/// \deprecated Use InfluxDBException instead - will be removed in v0.8.0
7575
class [[deprecated("Use InfluxDBException instead - will be removed in v0.8.0")]] ConnectionError : public InfluxDBException
7676
{
7777
public:
7878
ConnectionError(const std::string& source, const std::string& message)
79-
: InfluxDBException(source, message){};
79+
: InfluxDBException("influx-cxx [" + source + "]: " + message){};
8080
};
8181

8282

include/Transport.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ namespace influxdb
4949
/// Sends request
5050
virtual std::string query([[maybe_unused]] const std::string& query)
5151
{
52-
throw InfluxDBException{"Transport", "Queries are not supported by the selected transport"};
52+
throw InfluxDBException{"Queries are not supported by the selected transport"};
5353
}
5454

5555
/// Executes command
5656
virtual std::string execute([[maybe_unused]] const std::string& cmd)
5757
{
58-
throw InfluxDBException{"Transport", "Execution is not supported by the selected transport"};
58+
throw InfluxDBException{"Execution is not supported by the selected transport"};
5959
}
6060

6161
/// Sends request
6262
virtual void createDatabase()
6363
{
64-
throw InfluxDBException{"Transport", "Creation of database is not supported by the selected transport"};
64+
throw InfluxDBException{"Creation of database is not supported by the selected transport"};
6565
}
6666

6767
/// Sets proxy
6868
virtual void setProxy([[maybe_unused]] const Proxy& proxy)
6969
{
70-
throw InfluxDBException{"Transport", "Proxy is not supported by the selected transport"};
70+
throw InfluxDBException{"Proxy is not supported by the selected transport"};
7171
}
7272
};
7373

src/HTTP.cxx

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,12 @@ namespace influxdb::transports
3636
{
3737
if (resp.error)
3838
{
39-
throw ConnectionError(__func__, resp.error.message);
40-
}
41-
if (resp.status_code == 404)
42-
{
43-
throw NonExistentDatabase(__func__, "Nonexistent database: " + std::to_string(resp.status_code));
44-
}
45-
if (cpr::status::is_client_error(resp.status_code))
46-
{
47-
throw BadRequest(__func__, "Bad request: " + std::to_string(resp.status_code));
48-
}
49-
if (cpr::status::is_server_error(resp.status_code))
50-
{
51-
throw ServerError(__func__, "Influx server error: " + std::to_string(resp.status_code));
39+
// TODO: message may be empty here
40+
throw InfluxDBException{"Request error: (" + std::to_string(static_cast<int>(resp.error.code)) + ") " + resp.error.message};
5241
}
5342
if (!cpr::status::is_success(resp.status_code))
5443
{
55-
throw ConnectionError(__func__, "(" + std::to_string(resp.status_code) + ") " + resp.reason);
44+
throw InfluxDBException{"Request failed: (" + std::to_string(resp.status_code) + ") " + resp.reason};
5645
}
5746
}
5847

@@ -77,7 +66,7 @@ namespace influxdb::transports
7766

7867
if (dbParameterPosition == std::string::npos)
7968
{
80-
throw InfluxDBException{__func__, "Database not specified"};
69+
throw InfluxDBException{"No Database specified"};
8170
}
8271
return url.substr(dbParameterPosition + 4);
8372
}

src/InfluxDB.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace influxdb
4545
{
4646
if (mTransport == nullptr)
4747
{
48-
throw InfluxDBException{"[InfluxDB]", "Transport must not be nullptr"};
48+
throw InfluxDBException{"Transport must not be nullptr"};
4949
}
5050
}
5151

@@ -164,14 +164,7 @@ namespace influxdb
164164

165165
void InfluxDB::createDatabaseIfNotExists()
166166
{
167-
try
168-
{
169-
mTransport->createDatabase();
170-
}
171-
catch (const std::runtime_error&)
172-
{
173-
throw InfluxDBException(__func__, "Transport did not allow create database");
174-
}
167+
mTransport->createDatabase();
175168
}
176169

177170
} // namespace influxdb

src/InfluxDBFactory.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ namespace influxdb
6565
http::url parsedUrl = http::ParseHttpUrl(urlCopy);
6666
if (parsedUrl.protocol.empty())
6767
{
68-
throw InfluxDBException(__func__, "Ill-formed URI");
68+
throw InfluxDBException("Ill-formed URI");
6969
}
7070

7171
const auto iterator = map.find(parsedUrl.protocol);
7272
if (iterator == map.end())
7373
{
74-
throw InfluxDBException(__func__, "Unrecognized backend " + parsedUrl.protocol);
74+
throw InfluxDBException("Unrecognized backend " + parsedUrl.protocol);
7575
}
7676

7777
return iterator->second(parsedUrl);

src/NoBoostSupport.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ namespace influxdb::internal
2828
{
2929
std::vector<Point> queryImpl([[maybe_unused]] Transport* transport, [[maybe_unused]] const std::string& query)
3030
{
31-
throw InfluxDBException("InfluxDB", "Query requires Boost");
31+
throw InfluxDBException("Query requires Boost");
3232
}
3333

3434
std::unique_ptr<Transport> withUdpTransport([[maybe_unused]] const http::url& uri)
3535
{
36-
throw InfluxDBException("InfluxDBFactory", "UDP transport requires Boost");
36+
throw InfluxDBException("UDP transport requires Boost");
3737
}
3838

3939
std::unique_ptr<Transport> withTcpTransport([[maybe_unused]] const http::url& uri)
4040
{
41-
throw InfluxDBException("InfluxDBFactory", "TCP transport requires Boost");
41+
throw InfluxDBException("TCP transport requires Boost");
4242
}
4343

4444
std::unique_ptr<Transport> withUnixSocketTransport([[maybe_unused]] const http::url& uri)
4545
{
46-
throw InfluxDBException("InfluxDBFactory", "Unix socket transport requires Boost");
46+
throw InfluxDBException("Unix socket transport requires Boost");
4747
}
4848
}

src/TCP.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ namespace influxdb::transports
6363
const size_t written = mSocket.write_some(ba::buffer(message, message.size()));
6464
if (written != message.size())
6565
{
66-
throw InfluxDBException(__func__, "error while transmitting data");
66+
throw InfluxDBException("Error while transmitting data");
6767
}
6868
}
6969
catch (const boost::system::system_error& e)
7070
{
71-
throw InfluxDBException(__func__, e.what());
71+
throw InfluxDBException(e.what());
7272
}
7373
}
7474

src/UDP.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace influxdb::transports
4949
}
5050
catch (const boost::system::system_error& e)
5151
{
52-
throw InfluxDBException(__func__, e.what());
52+
throw InfluxDBException(e.what());
5353
}
5454
}
5555

src/UnixSocket.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ namespace influxdb::transports
4747
}
4848
catch (const boost::system::system_error& e)
4949
{
50-
throw InfluxDBException(__func__, e.what());
50+
throw InfluxDBException(e.what());
5151
}
5252
}
5353

5454
#else
5555

5656
UnixSocket::UnixSocket(const std::string&)
5757
{
58-
throw InfluxDBException{__func__, "Unix socket not supported on this system"};
58+
throw InfluxDBException{"Unix socket not supported on this system"};
5959
}
6060

6161
void UnixSocket::send(std::string&&)
6262
{
63-
throw InfluxDBException{__func__, "Unix socket not supported on this system"};
63+
throw InfluxDBException{"Unix socket not supported on this system"};
6464
}
6565

6666
#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)

test/BoostSupportTest.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace influxdb::test
9191
using trompeloeil::_;
9292

9393
TransportMock transport;
94-
ALLOW_CALL(transport, query(_)).THROW(InfluxDBException{"unit test", "Intentional"});
94+
ALLOW_CALL(transport, query(_)).THROW(InfluxDBException{"Intentional"});
9595

9696
CHECK_THROWS_AS(internal::queryImpl(&transport, "select should throw"), InfluxDBException);
9797
}

test/HttpTest.cxx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace influxdb::test
9999
ALLOW_CALL(sessionMock, SetBody(_));
100100
ALLOW_CALL(sessionMock, SetParameters(_));
101101

102-
REQUIRE_THROWS_AS(http.send("content"), ConnectionError);
102+
REQUIRE_THROWS_AS(http.send("content"), InfluxDBException);
103103
}
104104

105105
TEST_CASE("Send accepts successful response", "[HttpTest]")
@@ -125,7 +125,7 @@ namespace influxdb::test
125125
ALLOW_CALL(sessionMock, SetBody(_));
126126
ALLOW_CALL(sessionMock, SetParameters(_));
127127

128-
REQUIRE_THROWS_AS(http.send("content"), NonExistentDatabase);
128+
REQUIRE_THROWS_AS(http.send("content"), InfluxDBException);
129129
}
130130

131131
TEST_CASE("Query sets parameters", "[HttpTest]")
@@ -148,7 +148,7 @@ namespace influxdb::test
148148
ALLOW_CALL(sessionMock, SetUrl(_));
149149
ALLOW_CALL(sessionMock, SetParameters(_));
150150

151-
REQUIRE_THROWS_AS(http.query("/12?ab=cd"), ConnectionError);
151+
REQUIRE_THROWS_AS(http.query("/12?ab=cd"), InfluxDBException);
152152
}
153153

154154
TEST_CASE("Query accepts successful response", "[HttpTest]")
@@ -170,7 +170,7 @@ namespace influxdb::test
170170
ALLOW_CALL(sessionMock, SetUrl(_));
171171
ALLOW_CALL(sessionMock, SetParameters(_));
172172

173-
REQUIRE_THROWS_AS(http.query("/12?ab=cd"), ServerError);
173+
REQUIRE_THROWS_AS(http.query("/12?ab=cd"), InfluxDBException);
174174
}
175175

176176
TEST_CASE("Create database sets parameters", "[HttpTest]")
@@ -192,7 +192,7 @@ namespace influxdb::test
192192
ALLOW_CALL(sessionMock, SetUrl(_));
193193
ALLOW_CALL(sessionMock, SetParameters(_));
194194

195-
REQUIRE_THROWS_AS(http.createDatabase(), ConnectionError);
195+
REQUIRE_THROWS_AS(http.createDatabase(), InfluxDBException);
196196
}
197197

198198
TEST_CASE("Create database accepts successful response", "[HttpTest]")
@@ -214,7 +214,7 @@ namespace influxdb::test
214214
ALLOW_CALL(sessionMock, SetUrl(_));
215215
ALLOW_CALL(sessionMock, SetParameters(_));
216216

217-
REQUIRE_THROWS_AS(http.createDatabase(), ServerError);
217+
REQUIRE_THROWS_AS(http.createDatabase(), InfluxDBException);
218218
}
219219

220220
TEST_CASE("Set authentication sets parameters", "[HttpTest]")
@@ -263,7 +263,7 @@ namespace influxdb::test
263263
ALLOW_CALL(sessionMock, SetUrl(_));
264264
ALLOW_CALL(sessionMock, SetParameters(_));
265265

266-
REQUIRE_THROWS_AS(http.execute("fail-execution"), ConnectionError);
266+
REQUIRE_THROWS_AS(http.execute("fail-execution"), InfluxDBException);
267267
}
268268

269269
TEST_CASE("Execute accepts successful response", "[HttpTest]")
@@ -285,7 +285,7 @@ namespace influxdb::test
285285
ALLOW_CALL(sessionMock, SetUrl(_));
286286
ALLOW_CALL(sessionMock, SetParameters(_));
287287

288-
REQUIRE_THROWS_AS(http.execute("fail-execution"), NonExistentDatabase);
288+
REQUIRE_THROWS_AS(http.execute("fail-execution"), InfluxDBException);
289289
}
290290

291291
}

test/InfluxDBTest.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ namespace influxdb::test
167167
TEST_CASE("Create database throws if unsupported by transport", "[InfluxDBTest]")
168168
{
169169
auto mock = std::make_shared<TransportMock>();
170-
REQUIRE_CALL(*mock, createDatabase()).THROW(std::runtime_error{"Intentional"});
170+
REQUIRE_CALL(*mock, createDatabase()).THROW(InfluxDBException{"Intentional"});
171171

172172
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
173173
CHECK_THROWS_AS(db.createDatabaseIfNotExists(), InfluxDBException);

test/system/HttpAuthST.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace influxdb::test
3131
SECTION("Unauthenticated users fails")
3232
{
3333
auto db = configure("st_auth_db", {});
34-
CHECK_THROWS_AS(db->execute("show users"), BadRequest);
34+
CHECK_THROWS_AS(db->execute("show users"), InfluxDBException);
3535
}
3636

3737
SECTION("Authenticated user has access")

test/system/InfluxDBST.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ namespace influxdb::test
9797

9898
SECTION("Query point throws on invalid query")
9999
{
100-
CHECK_THROWS_AS(db->query("select* from_INVALID"), BadRequest);
100+
CHECK_THROWS_AS(db->query("select* from_INVALID"), InfluxDBException);
101101
}
102102

103103
SECTION("Write multiple points")
@@ -172,19 +172,19 @@ namespace influxdb::test
172172

173173
SECTION("Write of invalid line protocol throws")
174174
{
175-
CHECK_THROWS_AS(db->write(Point{"test,this=is ,,====,, invalid"}), BadRequest);
175+
CHECK_THROWS_AS(db->write(Point{"test,this=is ,,====,, invalid"}), InfluxDBException);
176176
}
177177

178178
SECTION("Write to unreachable host throws")
179179
{
180-
auto invalidDb = influxdb::InfluxDBFactory::Get("http://non_existing_host:123456?db=not_existing_db");
181-
CHECK_THROWS_AS(invalidDb->write(Point{"test"}.addField("value", 10)), ConnectionError);
180+
auto invalidDb = influxdb::InfluxDBFactory::Get("http://non_existing_host:1234?db=not_existing_db");
181+
CHECK_THROWS_AS(invalidDb->write(Point{"test"}.addField("value", 10)), InfluxDBException);
182182
}
183183

184184
SECTION("Write to nonexistent database throws")
185185
{
186186
auto invalidDb = configure("not_existing_db");
187-
CHECK_THROWS_AS(invalidDb->write(Point{"test"}.addField("value", 10)), NonExistentDatabase);
187+
CHECK_THROWS_AS(invalidDb->write(Point{"test"}.addField("value", 10)), InfluxDBException);
188188
}
189189

190190
SECTION("Cleanup")

0 commit comments

Comments
 (0)