Skip to content

Commit fa75f4e

Browse files
authored
Bugfix/catch exceptions (#24)
* removed empty catch blocks in tests * bugfix collection tests * bugfix ArangoDatabaseTest * bugfix ArangoEdgeCollectionTest * bugfix ArangoVertexCollectionTest
1 parent 4b07572 commit fa75f4e

File tree

11 files changed

+1612
-1617
lines changed

11 files changed

+1612
-1617
lines changed

src/main/java/com/arangodb/internal/ArangoExecutorAsync.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727

2828
import java.io.IOException;
2929
import java.lang.reflect.Type;
30-
import java.util.concurrent.CompletableFuture;
31-
import java.util.concurrent.ExecutorService;
32-
import java.util.concurrent.Executors;
33-
import java.util.function.Function;
30+
import java.util.concurrent.*;
3431

3532
/**
3633
* @author Mark Vollmary
@@ -64,8 +61,8 @@ public <T> CompletableFuture<T> execute(
6461
final ResponseDeserializer<T> responseDeserializer,
6562
final HostHandle hostHandle) {
6663

67-
return CompletableFuture.supplyAsync(() -> communication.execute(request, hostHandle), outgoingExecutor)
68-
.thenCompose(Function.identity())
64+
return CompletableFuture.completedFuture(null)
65+
.thenComposeAsync((it) -> communication.execute(request, hostHandle), outgoingExecutor)
6966
.thenApplyAsync(responseDeserializer::deserialize);
7067
}
7168

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.*;
3131
import java.util.concurrent.CompletableFuture;
32+
import java.util.concurrent.CompletionException;
3233
import java.util.concurrent.ExecutionException;
3334

3435
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -331,7 +332,8 @@ public void updateDocumentIfMatchFail() throws InterruptedException, ExecutionEx
331332
final DocumentUpdateOptions options = new DocumentUpdateOptions().ifMatch("no");
332333
db.collection(COLLECTION_NAME).updateDocument(createResult.getKey(), doc, options).get();
333334
fail();
334-
} catch (final Exception e) {
335+
} catch (final ExecutionException e) {
336+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
335337
}
336338
}
337339

@@ -508,7 +510,8 @@ public void updateDocumentIgnoreRevsFalse() throws InterruptedException, Executi
508510
final DocumentUpdateOptions options = new DocumentUpdateOptions().ignoreRevs(false);
509511
db.collection(COLLECTION_NAME).updateDocument(createResult.getKey(), doc, options).get();
510512
fail();
511-
} catch (final Exception e) {
513+
} catch (final ExecutionException e) {
514+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
512515
}
513516
}
514517

@@ -581,7 +584,8 @@ public void replaceDocumentIfMatchFail() throws InterruptedException, ExecutionE
581584
final DocumentReplaceOptions options = new DocumentReplaceOptions().ifMatch("no");
582585
db.collection(COLLECTION_NAME).replaceDocument(createResult.getKey(), doc, options).get();
583586
fail();
584-
} catch (final Exception e) {
587+
} catch (final ExecutionException e) {
588+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
585589
}
586590
}
587591

@@ -598,7 +602,8 @@ public void replaceDocumentIgnoreRevsFalse() throws InterruptedException, Execut
598602
final DocumentReplaceOptions options = new DocumentReplaceOptions().ignoreRevs(false);
599603
db.collection(COLLECTION_NAME).replaceDocument(createResult.getKey(), doc, options).get();
600604
fail();
601-
} catch (final Exception e) {
605+
} catch (final ExecutionException e) {
606+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
602607
}
603608
}
604609

@@ -703,7 +708,8 @@ public void deleteDocumentIfMatchFail() throws InterruptedException, ExecutionEx
703708
try {
704709
db.collection(COLLECTION_NAME).deleteDocument(createResult.getKey(), null, options).get();
705710
fail();
706-
} catch (final Exception e) {
711+
} catch (final ExecutionException e) {
712+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
707713
}
708714
}
709715

@@ -843,9 +849,7 @@ public void createGeo2Index() throws ExecutionException, InterruptedException {
843849
} else {
844850
assertThat(indexResult.getType(), is(IndexType.geo2));
845851
}
846-
} catch (InterruptedException e) {
847-
e.printStackTrace();
848-
} catch (ExecutionException e) {
852+
} catch (InterruptedException | ExecutionException e) {
849853
e.printStackTrace();
850854
}
851855
}).get();
@@ -1250,16 +1254,17 @@ public void importDocumentsDuplicateUpdate() throws InterruptedException, Execut
12501254
}
12511255

12521256
@Test
1253-
public void importDocumentsCompleteFail() {
1257+
public void importDocumentsCompleteFail() throws InterruptedException {
12541258
final Collection<BaseDocument> values = new ArrayList<>();
12551259
values.add(new BaseDocument("1"));
12561260
values.add(new BaseDocument("2"));
12571261
values.add(new BaseDocument("2"));
12581262
try {
12591263
db.collection(COLLECTION_NAME).importDocuments(values, new DocumentImportOptions().complete(true)).get();
12601264
fail();
1261-
} catch (InterruptedException | ExecutionException e) {
1262-
assertThat(e.getMessage(), containsString("1210"));
1265+
} catch (ExecutionException e) {
1266+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
1267+
assertThat(((ArangoDBException) e.getCause()).getErrorNum(), is(1210));
12631268
}
12641269
}
12651270

@@ -1434,13 +1439,14 @@ public void importDocumentsJsonDuplicateUpdate() throws InterruptedException, Ex
14341439
}
14351440

14361441
@Test
1437-
public void importDocumentsJsonCompleteFail() {
1442+
public void importDocumentsJsonCompleteFail() throws InterruptedException {
14381443
final String values = "[{\"_key\":\"1\"},{\"_key\":\"2\"},{\"_key\":\"2\"}]";
14391444
try {
14401445
db.collection(COLLECTION_NAME).importDocuments(values, new DocumentImportOptions().complete(true)).get();
14411446
fail();
1442-
} catch (InterruptedException | ExecutionException e) {
1443-
assertThat(e.getMessage(), containsString("1210"));
1447+
} catch (ExecutionException e) {
1448+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
1449+
assertThat(((ArangoDBException) e.getCause()).getErrorNum(), is(1210));
14441450
}
14451451
}
14461452

@@ -1887,7 +1893,8 @@ public void rename() throws InterruptedException, ExecutionException {
18871893
try {
18881894
db.collection(COLLECTION_NAME).getInfo().get();
18891895
fail();
1890-
} catch (final Exception e) {
1896+
} catch (final ExecutionException e) {
1897+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
18911898
}
18921899
} finally {
18931900
db.collection(COLLECTION_NAME + "1").rename(COLLECTION_NAME).get();

src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.io.IOException;
3636
import java.util.*;
3737
import java.util.concurrent.CompletableFuture;
38-
import java.util.concurrent.CompletionException;
3938
import java.util.concurrent.ExecutionException;
4039
import java.util.concurrent.atomic.AtomicInteger;
4140

@@ -181,7 +180,8 @@ public void deleteCollection() throws InterruptedException, ExecutionException {
181180
try {
182181
db.collection(COLLECTION_NAME).getInfo().get();
183182
fail();
184-
} catch (final Exception e) {
183+
} catch (final ExecutionException e) {
184+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
185185
}
186186
}
187187

@@ -196,7 +196,8 @@ public void deleteSystemCollection() throws InterruptedException, ExecutionExcep
196196
try {
197197
db.collection(name).getInfo().get();
198198
fail();
199-
} catch (final Exception e) {
199+
} catch (final ExecutionException e) {
200+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
200201
}
201202
}
202203

@@ -210,13 +211,15 @@ public void deleteSystemCollectionFail() throws InterruptedException, ExecutionE
210211
try {
211212
db.collection(name).drop().get();
212213
fail();
213-
} catch (final Exception e) {
214+
} catch (final ExecutionException e) {
215+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
214216
}
215217
db.collection(name).drop(true).get();
216218
try {
217219
db.collection(name).getInfo().get();
218220
fail();
219-
} catch (final Exception e) {
221+
} catch (final ExecutionException e) {
222+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
220223
}
221224
}
222225

@@ -563,9 +566,10 @@ public void queryWithTTL() throws InterruptedException, ExecutionException {
563566
Thread.sleep(wait * 1000);
564567
}
565568
}
566-
fail("this should fail");
569+
fail();
567570
} catch (final ArangoDBException ex) {
568-
assertThat(ex.getMessage(), is("Response: 404, Error: 1600 - cursor not found"));
571+
assertThat(ex.getResponseCode(), is(404));
572+
assertThat(ex.getErrorNum(), is(1600));
569573
} finally {
570574
db.collection(COLLECTION_NAME).drop().get();
571575
}
@@ -934,7 +938,8 @@ public void transactionallowImplicit() throws InterruptedException, ExecutionExc
934938
options.allowImplicit(false);
935939
db.transaction(action, VPackSlice.class, options).get();
936940
fail();
937-
} catch (final Exception e) {
941+
} catch (final ExecutionException e) {
942+
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
938943
}
939944
} finally {
940945
db.collection("someCollection").drop().get();
@@ -1046,13 +1051,13 @@ public void getDocument() throws InterruptedException, ExecutionException {
10461051
}
10471052

10481053
@Test
1049-
public void shouldIncludeExceptionMessage() {
1054+
public void shouldIncludeExceptionMessage() throws InterruptedException {
10501055
final String exceptionMessage = "My error context";
10511056
final String action = "function (params) {" + "throw '" + exceptionMessage + "';" + "}";
10521057
try {
1053-
db.transaction(action, VPackSlice.class, null).join();
1058+
db.transaction(action, VPackSlice.class, null).get();
10541059
fail();
1055-
} catch (final CompletionException e) {
1060+
} catch (final ExecutionException e) {
10561061
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
10571062
ArangoDBException cause = ((ArangoDBException) e.getCause());
10581063
assertThat(cause.getErrorMessage(), is(exceptionMessage));

0 commit comments

Comments
 (0)