Skip to content

Commit 01b63b3

Browse files
committed
ArangoDBException factory methods refactoring
1 parent 48f06fe commit 01b63b3

24 files changed

+101
-52
lines changed

core/src/main/java/com/arangodb/ArangoDBException.java

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

2323
import com.arangodb.entity.ErrorEntity;
2424

25+
import java.util.Objects;
2526
import java.util.concurrent.CompletionException;
2627

2728
/**
@@ -56,46 +57,94 @@ public ArangoDBException(final String message, final Integer responseCode) {
5657
this.requestId = null;
5758
}
5859

60+
/**
61+
* @deprecated use {@link com.arangodb.ArangoDBException#of(java.lang.Throwable)} instead
62+
*/
63+
@Deprecated
5964
public ArangoDBException(final Throwable cause) {
6065
super(cause);
6166
this.entity = null;
6267
this.responseCode = null;
6368
this.requestId = null;
6469
}
6570

71+
/**
72+
* @deprecated use {@link com.arangodb.ArangoDBException#of(String, Throwable)} instead
73+
*/
74+
@Deprecated
6675
public ArangoDBException(final String message, final Throwable cause) {
6776
super(message, cause);
6877
this.entity = null;
6978
this.responseCode = null;
7079
this.requestId = null;
7180
}
7281

82+
/**
83+
* @deprecated use {@link com.arangodb.ArangoDBException#of(Throwable, Long)} instead
84+
*/
85+
@Deprecated
7386
public ArangoDBException(Throwable cause, long requestId) {
7487
super(cause);
7588
this.entity = null;
7689
this.responseCode = null;
7790
this.requestId = requestId;
7891
}
7992

80-
private ArangoDBException(final ArangoDBException e) {
81-
super(e.getMessage(), e);
82-
this.entity = e.entity;
83-
this.responseCode = e.responseCode;
84-
this.requestId = e.requestId;
93+
private ArangoDBException(
94+
String message,
95+
Throwable cause,
96+
ErrorEntity entity,
97+
Integer responseCode,
98+
Long requestId
99+
) {
100+
super(message, cause);
101+
this.entity = entity;
102+
this.responseCode = responseCode;
103+
this.requestId = requestId;
104+
}
105+
106+
public static ArangoDBException of(Throwable t) {
107+
return of(null, t);
108+
}
109+
110+
public static ArangoDBException of(String message, Throwable t) {
111+
return of(message, t, null);
112+
}
113+
114+
public static ArangoDBException of(Throwable t, Long requestId) {
115+
return of(null, t, requestId);
85116
}
86117

87-
public static ArangoDBException wrap(Throwable t) {
118+
private static ArangoDBException of(String message, Throwable t, Long requestId) {
119+
Objects.requireNonNull(t);
120+
Throwable cause = unwrapCause(t);
121+
String msg = message != null ? message
122+
: t.getMessage() != null ? t.getMessage()
123+
: cause.getMessage();
124+
ErrorEntity entity = null;
125+
Integer responseCode = null;
126+
Long reqId = requestId;
127+
88128
if (t instanceof ArangoDBException) {
89-
if (t.getCause() == null) {
90-
return new ArangoDBException((ArangoDBException) t);
91-
} else {
92-
return wrap(t.getCause());
93-
}
94-
} else if (t instanceof CompletionException) {
95-
return wrap(t.getCause());
96-
} else {
97-
return new ArangoDBException(t);
129+
entity = ((ArangoDBException) t).entity;
130+
responseCode = ((ArangoDBException) t).responseCode;
131+
reqId = reqId != null ? reqId : ((ArangoDBException) t).requestId;
132+
}
133+
134+
return new ArangoDBException(
135+
msg,
136+
cause,
137+
entity,
138+
responseCode,
139+
reqId
140+
);
141+
}
142+
143+
private static Throwable unwrapCause(Throwable t) {
144+
if (t instanceof ArangoDBException || t instanceof CompletionException) {
145+
return unwrapCause(t.getCause());
98146
}
147+
return t;
99148
}
100149

101150
/**

core/src/main/java/com/arangodb/internal/ArangoCollectionAsyncImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ <T> T catchGetDocumentExceptions(Throwable err) {
334334
return null;
335335
}
336336
}
337-
throw ArangoDBException.wrap(e);
337+
throw ArangoDBException.of(e);
338338
}
339339

340340
@Override
@@ -405,7 +405,7 @@ public CompletableFuture<Boolean> exists() {
405405
return false;
406406
}
407407
}
408-
throw ArangoDBException.wrap(e);
408+
throw ArangoDBException.of(e);
409409
});
410410
}
411411

core/src/main/java/com/arangodb/internal/ArangoDatabaseAsyncImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public CompletableFuture<Boolean> exists() {
7979
}
8080
}
8181

82-
throw ArangoDBException.wrap(e);
82+
throw ArangoDBException.of(e);
8383
});
8484
}
8585

core/src/main/java/com/arangodb/internal/ArangoExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void disconnect() {
5050
try {
5151
protocol.close();
5252
} catch (final IOException e) {
53-
throw ArangoDBException.wrap(e);
53+
throw ArangoDBException.of(e);
5454
}
5555
}
5656

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public <T> CompletableFuture<T> execute(
5858
return protocol.executeAsync(interceptRequest(request), hostHandle)
5959
.handle((r, e) -> {
6060
if (e != null) {
61-
throw ArangoDBException.wrap(e);
61+
throw ArangoDBException.of(e);
6262
} else {
6363
interceptResponse(r);
6464
return responseDeserializer.deserialize(r);

core/src/main/java/com/arangodb/internal/ArangoGraphAsyncImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public CompletableFuture<Boolean> exists() {
5858
return false;
5959
}
6060
}
61-
throw ArangoDBException.wrap(e);
61+
throw ArangoDBException.of(e);
6262
});
6363
}
6464

core/src/main/java/com/arangodb/internal/ArangoSearchAsyncImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public CompletableFuture<Boolean> exists() {
5858
return false;
5959
}
6060
}
61-
throw ArangoDBException.wrap(e);
61+
throw ArangoDBException.of(e);
6262
});
6363
}
6464

core/src/main/java/com/arangodb/internal/ArangoViewAsyncImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CompletableFuture<Boolean> exists() {
5454
return false;
5555
}
5656
}
57-
throw ArangoDBException.wrap(e);
57+
throw ArangoDBException.of(e);
5858
});
5959
}
6060

core/src/main/java/com/arangodb/internal/SearchAliasAsyncImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public CompletableFuture<Boolean> exists() {
5858
return false;
5959
}
6060
}
61-
throw ArangoDBException.wrap(e);
61+
throw ArangoDBException.of(e);
6262
});
6363
}
6464

core/src/main/java/com/arangodb/internal/config/ArangoConfigPropertiesImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private Properties initProperties(String fileName) {
4848
try (InputStream is = getClass().getClassLoader().getResourceAsStream(fileName)) {
4949
p.load(is);
5050
} catch (Exception e) {
51-
throw new ArangoDBException("Got exception while reading properties file " + fileName, e);
51+
throw ArangoDBException.of("Got exception while reading properties file " + fileName, e);
5252
}
5353
return p;
5454
}

core/src/main/java/com/arangodb/internal/net/CommunicationProtocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ default InternalResponse execute(final InternalRequest request, final HostHandle
3838
return executeAsync(request, hostHandle).get();
3939
} catch (InterruptedException e) {
4040
Thread.currentThread().interrupt();
41-
throw ArangoDBException.wrap(e);
41+
throw ArangoDBException.of(e);
4242
} catch (ExecutionException e) {
43-
throw ArangoDBException.wrap(e.getCause());
43+
throw ArangoDBException.of(e.getCause());
4444
}
4545
}
4646

core/src/main/java/com/arangodb/internal/net/FallbackHostHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Host get(final HostHandle hostHandle, AccessType accessType) {
5454
if (current != lastSuccess || iterations < 3) {
5555
return current;
5656
} else {
57-
ArangoDBException e = new ArangoDBException("Cannot contact any host!",
57+
ArangoDBException e = ArangoDBException.of("Cannot contact any host!",
5858
new ArangoDBMultipleException(new ArrayList<>(lastFailExceptions)));
5959
reset();
6060
throw e;

core/src/main/java/com/arangodb/internal/net/HostImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void closeOnError() {
6060
try {
6161
connectionPool.close();
6262
} catch (final IOException e) {
63-
throw ArangoDBException.wrap(e);
63+
throw ArangoDBException.of(e);
6464
}
6565
}
6666

core/src/main/java/com/arangodb/internal/net/RoundRobinHostHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Host get(final HostHandle hostHandle, AccessType accessType) {
5454
final int size = hosts.getHostsList().size();
5555

5656
if (fails > size) {
57-
ArangoDBException e = new ArangoDBException("Cannot contact any host!",
57+
ArangoDBException e = ArangoDBException.of("Cannot contact any host!",
5858
new ArangoDBMultipleException(new ArrayList<>(lastFailExceptions)));
5959
reset();
6060
throw e;

core/src/main/java/com/arangodb/internal/serde/InternalSerdeImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public byte[] serialize(final Object value) {
5151
try {
5252
return mapper.writeValueAsBytes(value);
5353
} catch (JsonProcessingException e) {
54-
throw ArangoDBException.wrap(e);
54+
throw ArangoDBException.of(e);
5555
}
5656
}
5757

@@ -65,7 +65,7 @@ public String toJsonString(final byte[] content) {
6565
try {
6666
return SerdeUtils.INSTANCE.writeJson(mapper.readTree(content));
6767
} catch (IOException e) {
68-
throw ArangoDBException.wrap(e);
68+
throw ArangoDBException.of(e);
6969
}
7070
}
7171

@@ -75,7 +75,7 @@ public byte[] extract(final byte[] content, final String jsonPointer) {
7575
JsonNode target = parse(content).at(jsonPointer);
7676
return mapper.writeValueAsBytes(target);
7777
} catch (IOException e) {
78-
throw ArangoDBException.wrap(e);
78+
throw ArangoDBException.of(e);
7979
}
8080
}
8181

@@ -84,7 +84,7 @@ public JsonNode parse(byte[] content) {
8484
try {
8585
return mapper.readTree(content);
8686
} catch (IOException e) {
87-
throw ArangoDBException.wrap(e);
87+
throw ArangoDBException.of(e);
8888
}
8989
}
9090

@@ -93,7 +93,7 @@ public JsonNode parse(byte[] content, String jsonPointer) {
9393
try {
9494
return mapper.readTree(content).at(jsonPointer);
9595
} catch (IOException e) {
96-
throw ArangoDBException.wrap(e);
96+
throw ArangoDBException.of(e);
9797
}
9898
}
9999

@@ -153,7 +153,7 @@ public <T> T deserialize(final JsonNode node, final Type type) {
153153
try {
154154
return mapper.readerFor(mapper.constructType(type)).readValue(node);
155155
} catch (IOException e) {
156-
throw ArangoDBException.wrap(e);
156+
throw ArangoDBException.of(e);
157157
}
158158
}
159159

@@ -165,7 +165,7 @@ public <T> T deserialize(final byte[] content, final Type type) {
165165
try {
166166
return mapper.readerFor(mapper.constructType(type)).readValue(content);
167167
} catch (IOException e) {
168-
throw ArangoDBException.wrap(e);
168+
throw ArangoDBException.of(e);
169169
}
170170
}
171171

core/src/main/java/com/arangodb/internal/serde/SerdeUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public JsonNode parseJson(final String json) {
6565
try {
6666
return jsonMapper.readTree(json);
6767
} catch (JsonProcessingException e) {
68-
throw ArangoDBException.wrap(e);
68+
throw ArangoDBException.of(e);
6969
}
7070
}
7171

@@ -77,7 +77,7 @@ public String writeJson(final JsonNode data) {
7777
try {
7878
return jsonMapper.writeValueAsString(data);
7979
} catch (JsonProcessingException e) {
80-
throw ArangoDBException.wrap(e);
80+
throw ArangoDBException.of(e);
8181
}
8282
}
8383

core/src/main/java/com/arangodb/internal/util/EncodeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static String encodeURIComponent(final String value) {
4848
.replace("%29", ")")
4949
.replace("%7E", "~");
5050
} catch (UnsupportedEncodingException e) {
51-
throw ArangoDBException.wrap(e);
51+
throw ArangoDBException.of(e);
5252
}
5353
}
5454

core/src/main/java/com/arangodb/internal/util/ResponseUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void checkError(final InternalSerde util, final InternalResponse r
5353
final ErrorEntity errorEntity = util.deserialize(response.getBody(), ErrorEntity.class);
5454
ArangoDBException e = new ArangoDBException(errorEntity);
5555
if (ArangoErrors.QUEUE_TIME_VIOLATED.equals(e.getErrorNum())) {
56-
throw ArangoDBException.wrap(new TimeoutException().initCause(e));
56+
throw ArangoDBException.of(new TimeoutException().initCause(e));
5757
}
5858
throw e;
5959
} else {
@@ -72,7 +72,7 @@ public static ArangoDBException translateError(final InternalSerde util, final I
7272
final ErrorEntity errorEntity = util.deserialize(response.getBody(), ErrorEntity.class);
7373
ArangoDBException e = new ArangoDBException(errorEntity);
7474
if (ArangoErrors.QUEUE_TIME_VIOLATED.equals(e.getErrorNum())) {
75-
return ArangoDBException.wrap(new TimeoutException().initCause(e));
75+
return ArangoDBException.of(new TimeoutException().initCause(e));
7676
}
7777
return e;
7878
} else {

http/src/main/java/com/arangodb/http/HttpCommunication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ private CompletableFuture<InternalResponse> executeAsync(final InternalRequest r
8888
// SocketTimeoutException exceptions are wrapped and rethrown.
8989
TimeoutException te = new TimeoutException(e.getMessage());
9090
te.initCause(e);
91-
rfuture.completeExceptionally(new ArangoDBException(te, reqId));
91+
rfuture.completeExceptionally(ArangoDBException.of(te, reqId));
9292
} else if (e instanceof TimeoutException) {
93-
rfuture.completeExceptionally(new ArangoDBException(e, reqId));
93+
rfuture.completeExceptionally(ArangoDBException.of(e, reqId));
9494
} else if (e instanceof ConnectException) {
9595
handleException(true, e, hostHandle, request, host, reqId, attemptCount, rfuture);
9696
} else if (e != null) {
@@ -145,7 +145,7 @@ private void handleException(boolean isSafe, Throwable e, HostHandle hostHandle,
145145
rfuture
146146
);
147147
} else {
148-
ArangoDBException aEx = new ArangoDBException(ioEx, reqId);
148+
ArangoDBException aEx = ArangoDBException.of(ioEx, reqId);
149149
LOGGER.error(aEx.getMessage(), aEx);
150150
rfuture.completeExceptionally(aEx);
151151
}

http/src/main/java/com/arangodb/http/HttpConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private static String getUserAgent() {
134134
try {
135135
ctx = SSLContext.getDefault();
136136
} catch (NoSuchAlgorithmException e) {
137-
throw ArangoDBException.wrap(e);
137+
throw ArangoDBException.of(e);
138138
}
139139
}
140140

vst/src/main/java/com/arangodb/vst/VstCommunication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected synchronized C connect(final HostHandle hostHandle, final AccessType a
114114
failedHost.getDescription(), host.getDescription()));
115115
} else {
116116
LOGGER.error(e.getMessage(), e);
117-
throw ArangoDBException.wrap(e);
117+
throw ArangoDBException.of(e);
118118
}
119119
}
120120
}

0 commit comments

Comments
 (0)