Skip to content

Commit 54110d2

Browse files
authored
Update Neo4jException.gqlCause (#1608)
This update fixes `Neo4jException.gqlCause` that did not return subtypes of `Neo4jException`. In addition, only GQL causes from non interrupted error chains will be returned.
1 parent b70dc41 commit 54110d2

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

driver/src/main/java/org/neo4j/driver/exceptions/Neo4jException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ private static <T extends Throwable> Optional<T> findFirstGqlCause(Throwable thr
244244
if (cause == null) {
245245
return Optional.empty();
246246
}
247-
if (cause.getClass().isAssignableFrom(targetCls)) {
247+
if (targetCls.isAssignableFrom(cause.getClass())) {
248248
return Optional.of(targetCls.cast(cause));
249249
} else {
250-
return findFirstGqlCause(cause, targetCls);
250+
return Optional.empty();
251251
}
252252
}
253253
}

driver/src/test/java/org/neo4j/driver/exceptions/Neo4jExceptionTest.java

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,74 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

21-
import java.util.HashMap;
21+
import java.util.Collections;
22+
import java.util.Map;
23+
import java.util.stream.Stream;
2224
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
2328
import org.neo4j.driver.Value;
2429

2530
class Neo4jExceptionTest {
2631

27-
@Test
28-
void shouldInit() {
29-
var gqlStatus = "status";
30-
var description = "description";
31-
var code = "code";
32-
var message = "message";
33-
var map = new HashMap<String, Value>();
34-
var throwable = new Neo4jException(gqlStatus, description, code, message, map, null);
32+
@ParameterizedTest
33+
@MethodSource("shouldInitArgs")
34+
void shouldInit(
35+
String gqlStatus,
36+
String description,
37+
String code,
38+
String message,
39+
Map<String, Value> diagnosticRecord,
40+
Throwable cause) {
3541

36-
var exception = new Neo4jException(gqlStatus, description, code, message, map, throwable);
42+
var exception = new Neo4jException(gqlStatus, description, code, message, diagnosticRecord, cause);
3743

3844
assertEquals(gqlStatus, exception.gqlStatus());
3945
assertEquals(description, exception.statusDescription());
4046
assertEquals(code, exception.code());
4147
assertEquals(message, exception.getMessage());
42-
assertEquals(map, exception.diagnosticRecord());
43-
assertEquals(throwable, exception.gqlCause().orElse(null));
48+
assertEquals(diagnosticRecord, exception.diagnosticRecord());
49+
assertEquals(cause, exception.getCause());
50+
assertEquals(cause, exception.gqlCause().orElse(null));
51+
}
52+
53+
private static Stream<Arguments> shouldInitArgs() {
54+
return Stream.of(
55+
Arguments.of(
56+
"status",
57+
"description",
58+
"code",
59+
"message",
60+
Collections.emptyMap(),
61+
new Neo4jException("status", "description", "code", "message", Collections.emptyMap(), null)),
62+
Arguments.of(
63+
"status",
64+
"description",
65+
"code",
66+
"message",
67+
Collections.emptyMap(),
68+
new ServiceUnavailableException("message")));
69+
}
70+
71+
@Test
72+
void shouldFindGqlCauseOnNonInterruptedChainOnly() {
73+
var exception4 = new ServiceUnavailableException("message", null);
74+
var exception3 = new IllegalStateException("message", exception4);
75+
var exception2 =
76+
new Neo4jException("status", "description", "code", "message", Collections.emptyMap(), exception3);
77+
var exception1 =
78+
new Neo4jException("status", "description", "code", "message", Collections.emptyMap(), exception2);
79+
var exception =
80+
new ClientException("status", "description", "code", "message", Collections.emptyMap(), exception1);
81+
82+
assertError(exception, exception1, exception1);
83+
assertError(exception1, exception2, exception2);
84+
assertError(exception2, exception3, null);
85+
}
86+
87+
private void assertError(Neo4jException exception, Throwable expectedCause, Neo4jException expectedGqlCause) {
88+
assertEquals(expectedCause, exception.getCause());
89+
assertEquals(expectedGqlCause, exception.gqlCause().orElse(null));
4490
}
4591
}

0 commit comments

Comments
 (0)