Skip to content

Commit 86a4940

Browse files
author
Maximilian Hippler
committed
Fixed #602
1 parent 7e24523 commit 86a4940

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,19 @@
33

44
import com.squareup.moshi.JsonAdapter;
55
import com.squareup.moshi.Moshi;
6-
import okhttp3.Headers;
7-
import okhttp3.MediaType;
8-
import okhttp3.OkHttpClient;
9-
import okhttp3.Request;
10-
import okhttp3.RequestBody;
11-
import okhttp3.ResponseBody;
6+
import okhttp3.*;
127
import okhttp3.logging.HttpLoggingInterceptor;
138
import okhttp3.logging.HttpLoggingInterceptor.Level;
149
import okio.BufferedSource;
15-
1610
import org.influxdb.BatchOptions;
1711
import org.influxdb.InfluxDB;
1812
import org.influxdb.InfluxDBException;
1913
import org.influxdb.InfluxDBIOException;
20-
import org.influxdb.dto.BatchPoints;
21-
import org.influxdb.dto.BoundParameterQuery;
22-
import org.influxdb.dto.Point;
23-
import org.influxdb.dto.Pong;
24-
import org.influxdb.dto.Query;
25-
import org.influxdb.dto.QueryResult;
14+
import org.influxdb.dto.*;
2615
import org.influxdb.impl.BatchProcessor.HttpBatchEntry;
2716
import org.influxdb.impl.BatchProcessor.UdpBatchEntry;
2817
import org.influxdb.msgpack.MessagePackConverterFactory;
2918
import org.influxdb.msgpack.MessagePackTraverser;
30-
3119
import retrofit2.Call;
3220
import retrofit2.Callback;
3321
import retrofit2.Converter.Factory;
@@ -38,18 +26,11 @@
3826
import java.io.EOFException;
3927
import java.io.IOException;
4028
import java.io.InputStream;
41-
import java.net.DatagramPacket;
42-
import java.net.DatagramSocket;
43-
import java.net.InetAddress;
44-
import java.net.InetSocketAddress;
45-
import java.net.SocketException;
46-
import java.net.URI;
47-
import java.net.URISyntaxException;
48-
import java.net.UnknownHostException;
29+
import java.net.*;
4930
import java.nio.charset.StandardCharsets;
5031
import java.util.ArrayList;
51-
import java.util.Iterator;
5232
import java.util.Collections;
33+
import java.util.Iterator;
5334
import java.util.List;
5435
import java.util.concurrent.Executors;
5536
import java.util.concurrent.ThreadFactory;
@@ -561,7 +542,28 @@ public void query(final Query query, final Consumer<QueryResult> onSuccess, fina
561542
call.enqueue(new Callback<QueryResult>() {
562543
@Override
563544
public void onResponse(final Call<QueryResult> call, final Response<QueryResult> response) {
564-
onSuccess.accept(response.body());
545+
if (response.isSuccessful()) {
546+
onSuccess.accept(response.body());
547+
} else {
548+
Throwable t = null;
549+
String errorBody = null;
550+
551+
try {
552+
if (response.errorBody() != null) {
553+
errorBody = response.errorBody().string();
554+
}
555+
} catch (IOException e) {
556+
t = e;
557+
}
558+
559+
if (t != null) {
560+
onFailure.accept(new InfluxDBException(response.message(), t));
561+
} else if (errorBody != null) {
562+
onFailure.accept(new InfluxDBException(response.message() + " - " + errorBody));
563+
} else {
564+
onFailure.accept(new InfluxDBException(response.message()));
565+
}
566+
}
565567
}
566568

567569
@Override

src/test/java/org/influxdb/InfluxDBTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,38 +159,38 @@ public void testBoundParameterQuery() throws InterruptedException {
159159
}
160160
}
161161

162-
/**
163-
* Tests for callback query.
164-
*/
165-
@Test
166-
public void testCallbackQuery() throws Throwable {
167-
final AsyncResult<QueryResult> result = new AsyncResult<>();
168-
final Consumer<QueryResult> firstQueryConsumer = new Consumer<QueryResult>() {
169-
@Override
170-
public void accept(QueryResult queryResult) {
171-
influxDB.query(new Query("DROP DATABASE mydb2", "mydb"), result.resultConsumer, result.errorConsumer);
172-
}
173-
};
162+
/**
163+
* Tests for callback query.
164+
*/
165+
@Test
166+
public void testCallbackQuery() throws Throwable {
167+
final AsyncResult<QueryResult> result = new AsyncResult<>();
168+
final Consumer<QueryResult> firstQueryConsumer = new Consumer<QueryResult>() {
169+
@Override
170+
public void accept(QueryResult queryResult) {
171+
influxDB.query(new Query("DROP DATABASE mydb2", "mydb"), result.resultConsumer, result.errorConsumer);
172+
}
173+
};
174174

175-
this.influxDB.query(new Query("CREATE DATABASE mydb2", "mydb"), firstQueryConsumer, result.errorConsumer);
175+
this.influxDB.query(new Query("CREATE DATABASE mydb2", "mydb"), firstQueryConsumer, result.errorConsumer);
176176

177-
// Will throw exception in case of error.
178-
result.result();
179-
}
177+
// Will throw exception in case of error.
178+
result.result();
179+
}
180180

181-
/**
182-
* Tests for callback query with a failure.
183-
* see Issue #602
184-
*/
185-
@Test
186-
public void testCallbackQueryFailureHandling() {
187-
final AsyncResult<QueryResult> res = new AsyncResult<>();
181+
/**
182+
* Tests for callback query with a failure.
183+
* see Issue #602
184+
*/
185+
@Test
186+
public void testCallbackQueryFailureHandling() {
187+
final AsyncResult<QueryResult> res = new AsyncResult<>();
188188

189-
this.influxDB.query(new Query("SHOW SERRIES"), res.resultConsumer, res.errorConsumer);
189+
this.influxDB.query(new Query("SHOW SERRIES"), res.resultConsumer, res.errorConsumer);
190190

191-
Assertions.assertThrows(InfluxDBException.class, res::result,
192-
"Malformed query should throw InfluxDBException");
193-
}
191+
Assertions.assertThrows(InfluxDBException.class, res::result,
192+
"Malformed query should throw InfluxDBException");
193+
}
194194

195195
/**
196196
* Test that describe Databases works.

0 commit comments

Comments
 (0)