Skip to content

Commit 2faba3a

Browse files
GH-2248 - Don't swallow or delete cause of translated exceptions.
This fixes #2248.
1 parent c12fb7b commit 2faba3a

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jPersistenceExceptionTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static DataAccessException translateImpl(Neo4jException e,
102102
String msg = String.format("%s; Error code '%s'", e.getMessage(), optionalErrorCode.orElse("n/a"));
103103

104104
return optionalErrorCode.flatMap(code -> ERROR_CODE_MAPPINGS.getOrDefault(code, Optional.empty()))
105-
.orElse(defaultTranslationProvider).apply(msg, e.getCause());
105+
.orElse(defaultTranslationProvider).apply(msg, e);
106106
}
107107

108108
static {

src/test/java/org/springframework/data/neo4j/core/Neo4jPersistenceExceptionTranslatorTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void shouldHandleNullErrorCode() {
3636
DataAccessException dataAccessException = translator.translateExceptionIfPossible(new LossyCoercion("Long", "Int"));
3737
assertThat(dataAccessException).isNotNull().isInstanceOf(InvalidDataAccessApiUsageException.class);
3838
assertThat(dataAccessException.getMessage())
39-
.isEqualTo("Cannot coerce Long to Int without losing precision; Error code 'N/A'");
39+
.startsWith("Cannot coerce Long to Int without losing precision; Error code 'N/A'");
4040
}
4141

4242
@Test
@@ -47,6 +47,6 @@ void shouldKeepErrorCodeIntact() {
4747
new ClientException("Neo.ClientError.Statement.EntityNotFound", "Something went wrong."));
4848
assertThat(dataAccessException).isNotNull().isInstanceOf(InvalidDataAccessResourceUsageException.class);
4949
assertThat(dataAccessException.getMessage())
50-
.isEqualTo("Something went wrong.; Error code 'Neo.ClientError.Statement.EntityNotFound'");
50+
.startsWith("Something went wrong.; Error code 'Neo.ClientError.Statement.EntityNotFound'");
5151
}
5252
}

src/test/java/org/springframework/data/neo4j/core/support/RetryExceptionPredicateTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.neo4j.driver.exceptions.ServiceUnavailableException;
2525
import org.neo4j.driver.exceptions.SessionExpiredException;
2626
import org.springframework.dao.TransientDataAccessResourceException;
27+
import org.springframework.data.neo4j.core.Neo4jPersistenceExceptionTranslator;
2728

2829
/**
2930
* @author Michael J. Simons
@@ -47,6 +48,15 @@ void shouldNotRetryOnRandomIllegalStateExceptions() {
4748
assertThat(predicate.test(new IllegalStateException())).isFalse();
4849
}
4950

51+
@Test
52+
void shouldPlayWellWithExceptionTranslator() {
53+
54+
Neo4jPersistenceExceptionTranslator translator = new Neo4jPersistenceExceptionTranslator();
55+
Exception e = translator.translateExceptionIfPossible(new SessionExpiredException("Schade"));
56+
RetryExceptionPredicate predicate = new RetryExceptionPredicate();
57+
assertThat(predicate.test(e)).isTrue();
58+
}
59+
5060
@ParameterizedTest
5161
@ValueSource(classes = { SessionExpiredException.class, ServiceUnavailableException.class })
5262
void shouldRetryOnObviousRetryableExceptions(Class<? extends Exception> typeOfException) {

src/test/java/org/springframework/data/neo4j/integration/imperative/ExceptionTranslationIT.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void exceptionsFromClientShouldBeTranslated(@Autowired Neo4jClient neo4jClient)
8383

8484
assertThatExceptionOfType(DataIntegrityViolationException.class)
8585
.isThrownBy(() -> neo4jClient.query("CREATE (:SimplePerson {name: 'Tom'})").run()).withMessageMatching(
86-
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed'");
86+
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed';.*");
8787
}
8888

8989
@Test
@@ -92,7 +92,7 @@ void exceptionsFromRepositoriesShouldBeTranslated(@Autowired SimplePersonReposit
9292

9393
assertThatExceptionOfType(DataIntegrityViolationException.class)
9494
.isThrownBy(() -> repository.save(new SimplePerson("Jerry"))).withMessageMatching(
95-
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed'");
95+
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed';.*");
9696
}
9797

9898
/*
@@ -105,7 +105,7 @@ void exceptionsOnRepositoryBeansShouldBeTranslated(@Autowired CustomDAO customDA
105105

106106
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> customDAO.createPerson())
107107
.withMessageMatching(
108-
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed'");
108+
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed';.*");
109109
}
110110

111111
interface SimplePersonRepository extends Neo4jRepository<SimplePerson, Long> {}

src/test/java/org/springframework/data/neo4j/integration/reactive/ReactiveExceptionTranslationIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ReactiveExceptionTranslationIT {
5858
// @formatter:off
5959
private final Predicate<Throwable> aTranslatedException = ex -> ex instanceof DataIntegrityViolationException && //
6060
ex.getMessage().matches(
61-
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed'");
61+
"Node\\(\\d+\\) already exists with label `SimplePerson` and property `name` = '[\\w\\s]+'; Error code 'Neo.ClientError.Schema.ConstraintValidationFailed';.*");
6262
// @formatter:on
6363

6464
@BeforeAll

0 commit comments

Comments
 (0)