Skip to content

Wrap checked exceptions when rethrowing async exception #1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @since 1.0
*/
public abstract class Neo4jException extends RuntimeException {
public class Neo4jException extends RuntimeException {
private static final long serialVersionUID = -80579062276712566L;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.neo4j.driver.internal.util;

import io.netty.util.internal.PlatformDependent;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
Expand Down Expand Up @@ -115,7 +114,13 @@ public static void rethrowAsyncException(ExecutionException e) {
.toArray(StackTraceElement[]::new);
error.setStackTrace(currentStackTrace);

PlatformDependent.throwException(error);
RuntimeException exception;
if (error instanceof RuntimeException) {
exception = (RuntimeException) error;
} else {
exception = new Neo4jException("Driver execution failed", error);
}
throw exception;
}

private static boolean isProtocolViolationError(Neo4jException error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
import static org.hamcrest.junit.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.neo4j.driver.internal.util.ErrorUtil.isFatal;
import static org.neo4j.driver.internal.util.ErrorUtil.newConnectionTerminatedError;
import static org.neo4j.driver.internal.util.ErrorUtil.newNeo4jError;
import static org.neo4j.driver.internal.util.ErrorUtil.rethrowAsyncException;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.exceptions.AuthenticationException;
import org.neo4j.driver.exceptions.AuthorizationExpiredException;
Expand Down Expand Up @@ -195,4 +201,16 @@ void shouldMapTransientTransactionLockClientStoppedToClientException() {
assertEquals("Neo.ClientError.Transaction.LockClientStopped", error.code());
assertEquals(message, error.getMessage());
}

@Test
void shouldWrapCheckedExceptionsInNeo4jExceptionWhenRethrowingAsyncException() {
ExecutionException ee = mock(ExecutionException.class);
UnknownHostException uhe = mock(UnknownHostException.class);
given(ee.getCause()).willReturn(uhe);
given(uhe.getStackTrace()).willReturn(new StackTraceElement[0]);

Neo4jException actual = assertThrows(Neo4jException.class, () -> rethrowAsyncException(ee));

assertEquals(actual.getCause(), uhe);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.exceptions.Neo4jException;
import org.neo4j.driver.internal.async.connection.EventLoopGroupFactory;

class FuturesTest {
Expand Down Expand Up @@ -177,8 +178,8 @@ void shouldThrowInBlockingGetWhenFutureThrowsCheckedException() {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(error);

Exception e = assertThrows(Exception.class, () -> Futures.blockingGet(future));
assertEquals(error, e);
Neo4jException e = assertThrows(Neo4jException.class, () -> Futures.blockingGet(future));
assertEquals(error, e.getCause());
}

@Test
Expand Down