Skip to content

Update Neo4jException.gqlCause #1608

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
Jan 28, 2025
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 @@ -244,10 +244,10 @@ private static <T extends Throwable> Optional<T> findFirstGqlCause(Throwable thr
if (cause == null) {
return Optional.empty();
}
if (cause.getClass().isAssignableFrom(targetCls)) {
if (targetCls.isAssignableFrom(cause.getClass())) {
return Optional.of(targetCls.cast(cause));
} else {
return findFirstGqlCause(cause, targetCls);
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,74 @@

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

import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.neo4j.driver.Value;

class Neo4jExceptionTest {

@Test
void shouldInit() {
var gqlStatus = "status";
var description = "description";
var code = "code";
var message = "message";
var map = new HashMap<String, Value>();
var throwable = new Neo4jException(gqlStatus, description, code, message, map, null);
@ParameterizedTest
@MethodSource("shouldInitArgs")
void shouldInit(
String gqlStatus,
String description,
String code,
String message,
Map<String, Value> diagnosticRecord,
Throwable cause) {

var exception = new Neo4jException(gqlStatus, description, code, message, map, throwable);
var exception = new Neo4jException(gqlStatus, description, code, message, diagnosticRecord, cause);

assertEquals(gqlStatus, exception.gqlStatus());
assertEquals(description, exception.statusDescription());
assertEquals(code, exception.code());
assertEquals(message, exception.getMessage());
assertEquals(map, exception.diagnosticRecord());
assertEquals(throwable, exception.gqlCause().orElse(null));
assertEquals(diagnosticRecord, exception.diagnosticRecord());
assertEquals(cause, exception.getCause());
assertEquals(cause, exception.gqlCause().orElse(null));
}

private static Stream<Arguments> shouldInitArgs() {
return Stream.of(
Arguments.of(
"status",
"description",
"code",
"message",
Collections.emptyMap(),
new Neo4jException("status", "description", "code", "message", Collections.emptyMap(), null)),
Arguments.of(
"status",
"description",
"code",
"message",
Collections.emptyMap(),
new ServiceUnavailableException("message")));
}

@Test
void shouldFindGqlCauseOnNonInterruptedChainOnly() {
var exception4 = new ServiceUnavailableException("message", null);
var exception3 = new IllegalStateException("message", exception4);
var exception2 =
new Neo4jException("status", "description", "code", "message", Collections.emptyMap(), exception3);
var exception1 =
new Neo4jException("status", "description", "code", "message", Collections.emptyMap(), exception2);
var exception =
new ClientException("status", "description", "code", "message", Collections.emptyMap(), exception1);

assertError(exception, exception1, exception1);
assertError(exception1, exception2, exception2);
assertError(exception2, exception3, null);
}

private void assertError(Neo4jException exception, Throwable expectedCause, Neo4jException expectedGqlCause) {
assertEquals(expectedCause, exception.getCause());
assertEquals(expectedGqlCause, exception.gqlCause().orElse(null));
}
}