Skip to content

Commit 546e976

Browse files
authored
Merge pull request #323 from simon04/influxdb-exceptions
Replace RuntimeException with InfluxDBException
2 parents fb09493 + 4961f31 commit 546e976

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.influxdb;
2+
3+
/**
4+
* A wrapper for various exceptions caused while interacting with InfluxDB.
5+
*
6+
* @author Simon Legner
7+
*/
8+
public class InfluxDBException extends RuntimeException {
9+
10+
public InfluxDBException(final String message) {
11+
super(message);
12+
}
13+
14+
public InfluxDBException(final String message, final Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public InfluxDBException(final Throwable cause) {
19+
super(cause);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.influxdb;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* A wrapper for {@link IOException} caused while interacting with InfluxDB.
7+
*
8+
* @author Simon Legner
9+
*/
10+
public class InfluxDBIOException extends InfluxDBException {
11+
12+
public InfluxDBIOException(final IOException cause) {
13+
super(cause);
14+
}
15+
}

src/main/java/org/influxdb/dto/Query.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static String encode(final String command) {
105105
try {
106106
return URLEncoder.encode(command, StandardCharsets.UTF_8.name());
107107
} catch (UnsupportedEncodingException e) {
108-
throw new RuntimeException(e);
108+
throw new IllegalStateException("Every JRE must support UTF-8", e);
109109
}
110110
}
111111
}

src/main/java/org/influxdb/impl/InfluxDBImpl.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.squareup.moshi.Moshi;
1111

1212
import org.influxdb.InfluxDB;
13+
import org.influxdb.InfluxDBException;
14+
import org.influxdb.InfluxDBIOException;
1315
import org.influxdb.dto.BatchPoints;
1416
import org.influxdb.dto.Point;
1517
import org.influxdb.dto.Pong;
@@ -105,7 +107,7 @@ private InetAddress parseHostAddress(final String url) {
105107
try {
106108
return InetAddress.getByName(httpUrl.host());
107109
} catch (UnknownHostException e) {
108-
throw new RuntimeException(e);
110+
throw new InfluxDBIOException(e);
109111
}
110112
}
111113

@@ -227,7 +229,7 @@ public Pong ping() {
227229
pong.setResponseTime(watch.elapsed(TimeUnit.MILLISECONDS));
228230
return pong;
229231
} catch (IOException e) {
230-
throw new RuntimeException(e);
232+
throw new InfluxDBIOException(e);
231233
}
232234
}
233235

@@ -310,7 +312,7 @@ public void write(final int udpPort, final String records) {
310312
try {
311313
datagramSocket.send(new DatagramPacket(bytes, bytes.length, hostAddress, udpPort));
312314
} catch (IOException e) {
313-
throw new RuntimeException(e);
315+
throw new InfluxDBIOException(e);
314316
}
315317
}
316318

@@ -321,7 +323,7 @@ private void initialDatagramSocket() {
321323
try {
322324
datagramSocket = new DatagramSocket();
323325
} catch (SocketException e) {
324-
throw new RuntimeException(e);
326+
throw new InfluxDBIOException(e);
325327
}
326328
}
327329
}
@@ -360,7 +362,7 @@ public QueryResult query(final Query query) {
360362
public void query(final Query query, final int chunkSize, final Consumer<QueryResult> consumer) {
361363

362364
if (version().startsWith("0.") || version().startsWith("1.0")) {
363-
throw new RuntimeException("chunking not supported");
365+
throw new UnsupportedOperationException("chunking not supported");
364366
}
365367

366368
Call<ResponseBody> call = this.influxDBService.query(this.username, this.password,
@@ -380,20 +382,20 @@ public void onResponse(final Call<ResponseBody> call, final Response<ResponseBod
380382
}
381383
}
382384
try (ResponseBody errorBody = response.errorBody()) {
383-
throw new RuntimeException(errorBody.string());
385+
throw new InfluxDBException(errorBody.string());
384386
}
385387
} catch (EOFException e) {
386388
QueryResult queryResult = new QueryResult();
387389
queryResult.setError("DONE");
388390
consumer.accept(queryResult);
389391
} catch (IOException e) {
390-
throw new RuntimeException(e);
392+
throw new InfluxDBIOException(e);
391393
}
392394
}
393395

394396
@Override
395397
public void onFailure(final Call<ResponseBody> call, final Throwable t) {
396-
throw new RuntimeException(t);
398+
throw new InfluxDBException(t);
397399
}
398400
});
399401
}
@@ -469,10 +471,10 @@ private <T> T execute(final Call<T> call) {
469471
return response.body();
470472
}
471473
try (ResponseBody errorBody = response.errorBody()) {
472-
throw new RuntimeException(errorBody.string());
474+
throw new InfluxDBException(errorBody.string());
473475
}
474476
} catch (IOException e) {
475-
throw new RuntimeException(e);
477+
throw new InfluxDBIOException(e);
476478
}
477479
}
478480

0 commit comments

Comments
 (0)