Skip to content

Commit 7c7a30a

Browse files
committed
Wrap checked exceptions when rethrowing async exception
Driver may throw checked exception without declaring it. This update fixes this behaviour.
1 parent 30f5ed3 commit 7c7a30a

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @since 1.0
2525
*/
26-
public abstract class Neo4jException extends RuntimeException {
26+
public class Neo4jException extends RuntimeException {
2727
private static final long serialVersionUID = -80579062276712566L;
2828

2929
private final String code;

driver/src/main/java/org/neo4j/driver/internal/util/ErrorUtil.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.neo4j.driver.internal.util;
2020

21-
import io.netty.util.internal.PlatformDependent;
2221
import java.util.Objects;
2322
import java.util.concurrent.ExecutionException;
2423
import java.util.stream.Stream;
@@ -115,7 +114,13 @@ public static void rethrowAsyncException(ExecutionException e) {
115114
.toArray(StackTraceElement[]::new);
116115
error.setStackTrace(currentStackTrace);
117116

118-
PlatformDependent.throwException(error);
117+
RuntimeException exception;
118+
if (error instanceof RuntimeException) {
119+
exception = (RuntimeException) error;
120+
} else {
121+
exception = new Neo4jException("Driver execution failed", error);
122+
}
123+
throw exception;
119124
}
120125

121126
private static boolean isProtocolViolationError(Neo4jException error) {

driver/src/test/java/org/neo4j/driver/internal/util/ErrorUtilTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@
2424
import static org.hamcrest.junit.MatcherAssert.assertThat;
2525
import static org.junit.jupiter.api.Assertions.assertEquals;
2626
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
2728
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.mock;
2831
import static org.neo4j.driver.internal.util.ErrorUtil.isFatal;
2932
import static org.neo4j.driver.internal.util.ErrorUtil.newConnectionTerminatedError;
3033
import static org.neo4j.driver.internal.util.ErrorUtil.newNeo4jError;
34+
import static org.neo4j.driver.internal.util.ErrorUtil.rethrowAsyncException;
3135

3236
import java.io.IOException;
37+
import java.net.UnknownHostException;
38+
import java.util.concurrent.ExecutionException;
3339
import org.junit.jupiter.api.Test;
3440
import org.neo4j.driver.exceptions.AuthenticationException;
3541
import org.neo4j.driver.exceptions.AuthorizationExpiredException;
@@ -195,4 +201,16 @@ void shouldMapTransientTransactionLockClientStoppedToClientException() {
195201
assertEquals("Neo.ClientError.Transaction.LockClientStopped", error.code());
196202
assertEquals(message, error.getMessage());
197203
}
204+
205+
@Test
206+
void shouldWrapCheckedExceptionsInNeo4jExceptionWhenRethrowingAsyncException() {
207+
ExecutionException ee = mock(ExecutionException.class);
208+
UnknownHostException uhe = mock(UnknownHostException.class);
209+
given(ee.getCause()).willReturn(uhe);
210+
given(uhe.getStackTrace()).willReturn(new StackTraceElement[0]);
211+
212+
Neo4jException actual = assertThrows(Neo4jException.class, () -> rethrowAsyncException(ee));
213+
214+
assertEquals(actual.getCause(), uhe);
215+
}
198216
}

driver/src/test/java/org/neo4j/driver/internal/util/FuturesTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.concurrent.ExecutorService;
4545
import java.util.concurrent.Executors;
4646
import org.junit.jupiter.api.Test;
47+
import org.neo4j.driver.exceptions.Neo4jException;
4748
import org.neo4j.driver.internal.async.connection.EventLoopGroupFactory;
4849

4950
class FuturesTest {
@@ -177,8 +178,8 @@ void shouldThrowInBlockingGetWhenFutureThrowsCheckedException() {
177178
CompletableFuture<String> future = new CompletableFuture<>();
178179
future.completeExceptionally(error);
179180

180-
Exception e = assertThrows(Exception.class, () -> Futures.blockingGet(future));
181-
assertEquals(error, e);
181+
Neo4jException e = assertThrows(Neo4jException.class, () -> Futures.blockingGet(future));
182+
assertEquals(error, e.getCause());
182183
}
183184

184185
@Test

0 commit comments

Comments
 (0)