Skip to content

Commit cc469d4

Browse files
authored
Merge pull request #603 from maxemann96/Issue602
Closes #602
2 parents 3169621 + 69d5fbd commit cc469d4

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.influxdb.impl;
22

3-
43
import com.squareup.moshi.JsonAdapter;
54
import com.squareup.moshi.Moshi;
65
import okhttp3.Headers;
@@ -12,7 +11,6 @@
1211
import okhttp3.logging.HttpLoggingInterceptor;
1312
import okhttp3.logging.HttpLoggingInterceptor.Level;
1413
import okio.BufferedSource;
15-
1614
import org.influxdb.BatchOptions;
1715
import org.influxdb.InfluxDB;
1816
import org.influxdb.InfluxDBException;
@@ -27,7 +25,6 @@
2725
import org.influxdb.impl.BatchProcessor.UdpBatchEntry;
2826
import org.influxdb.msgpack.MessagePackConverterFactory;
2927
import org.influxdb.msgpack.MessagePackTraverser;
30-
3128
import retrofit2.Call;
3229
import retrofit2.Callback;
3330
import retrofit2.Converter.Factory;
@@ -48,8 +45,8 @@
4845
import java.net.UnknownHostException;
4946
import java.nio.charset.StandardCharsets;
5047
import java.util.ArrayList;
51-
import java.util.Iterator;
5248
import java.util.Collections;
49+
import java.util.Iterator;
5350
import java.util.List;
5451
import java.util.concurrent.Executors;
5552
import java.util.concurrent.ThreadFactory;
@@ -561,7 +558,28 @@ public void query(final Query query, final Consumer<QueryResult> onSuccess, fina
561558
call.enqueue(new Callback<QueryResult>() {
562559
@Override
563560
public void onResponse(final Call<QueryResult> call, final Response<QueryResult> response) {
564-
onSuccess.accept(response.body());
561+
if (response.isSuccessful()) {
562+
onSuccess.accept(response.body());
563+
} else {
564+
Throwable t = null;
565+
String errorBody = null;
566+
567+
try {
568+
if (response.errorBody() != null) {
569+
errorBody = response.errorBody().string();
570+
}
571+
} catch (IOException e) {
572+
t = e;
573+
}
574+
575+
if (t != null) {
576+
onFailure.accept(new InfluxDBException(response.message(), t));
577+
} else if (errorBody != null) {
578+
onFailure.accept(new InfluxDBException(response.message() + " - " + errorBody));
579+
} else {
580+
onFailure.accept(new InfluxDBException(response.message()));
581+
}
582+
}
565583
}
566584

567585
@Override

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

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.influxdb;
22

3-
import java.util.Collections;
3+
import okhttp3.OkHttpClient;
44
import org.influxdb.InfluxDB.LogLevel;
55
import org.influxdb.InfluxDB.ResponseFormat;
66
import org.influxdb.dto.BatchPoints;
@@ -19,15 +19,14 @@
1919
import org.junit.platform.runner.JUnitPlatform;
2020
import org.junit.runner.RunWith;
2121

22-
import okhttp3.OkHttpClient;
23-
2422
import java.io.IOException;
2523
import java.net.ConnectException;
2624
import java.time.Instant;
2725
import java.time.ZoneId;
2826
import java.time.format.DateTimeFormatter;
2927
import java.util.ArrayList;
3028
import java.util.Arrays;
29+
import java.util.Collections;
3130
import java.util.List;
3231
import java.util.Set;
3332
import java.util.concurrent.BlockingQueue;
@@ -40,6 +39,7 @@
4039
import java.util.concurrent.TimeUnit;
4140
import java.util.concurrent.atomic.LongAdder;
4241
import java.util.function.Consumer;
42+
import java.util.regex.Pattern;
4343

4444
/**
4545
* Test the InfluxDB API.
@@ -175,24 +175,47 @@ public void testBoundParameterQuery() throws InterruptedException {
175175
}
176176
}
177177

178-
/**
179-
* Tests for callback query.
180-
*/
181-
@Test
182-
public void testCallbackQuery() throws Throwable {
183-
final AsyncResult<QueryResult> result = new AsyncResult<>();
184-
final Consumer<QueryResult> firstQueryConsumer = new Consumer<QueryResult>() {
185-
@Override
186-
public void accept(QueryResult queryResult) {
187-
influxDB.query(new Query("DROP DATABASE mydb2", "mydb"), result.resultConsumer, result.errorConsumer);
188-
}
189-
};
178+
/**
179+
* Tests for callback query.
180+
*/
181+
@Test
182+
public void testCallbackQuery() throws Throwable {
183+
final AsyncResult<QueryResult> result = new AsyncResult<>();
184+
final Consumer<QueryResult> firstQueryConsumer = new Consumer<QueryResult>() {
185+
@Override
186+
public void accept(QueryResult queryResult) {
187+
influxDB.query(new Query("DROP DATABASE mydb2", "mydb"), result.resultConsumer, result.errorConsumer);
188+
}
189+
};
190190

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

193-
// Will throw exception in case of error.
194-
result.result();
195-
}
193+
// Will throw exception in case of error.
194+
result.result();
195+
}
196+
197+
/**
198+
* Tests for callback query with a failure.
199+
* see Issue #602
200+
*/
201+
@Test
202+
public void testCallbackQueryFailureHandling() throws Throwable {
203+
final AsyncResult<QueryResult> res = new AsyncResult<>();
204+
205+
this.influxDB.query(new Query("SHOW SERRIES"), res.resultConsumer, res.errorConsumer);
206+
207+
try{
208+
res.result();
209+
Assertions.fail("Malformed query should throw InfluxDBException");
210+
}
211+
catch (InfluxDBException e){
212+
Pattern errorPattern = Pattern.compile("Bad Request.*error parsing query: found SERRIES, expected.*",
213+
Pattern.DOTALL);
214+
215+
Assertions.assertTrue(errorPattern.matcher(e.getMessage()).matches(),
216+
"Error string \"" + e.getMessage() + "\" does not match error pattern");
217+
}
218+
}
196219

197220
/**
198221
* Test that describe Databases works.

0 commit comments

Comments
 (0)