-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-611 added JdbcExceptionTranslator
- Loading branch information
Showing
12 changed files
with
206 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...g-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcExceptionTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.springframework.data.jdbc.core; | ||
|
||
import java.util.Set; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.dao.DuplicateKeyException; | ||
import org.springframework.dao.OptimisticLockingFailureException; | ||
import org.springframework.dao.support.PersistenceExceptionTranslator; | ||
|
||
/** | ||
* {@link PersistenceExceptionTranslator} that is capable to translate exceptions for JDBC module. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public class JdbcExceptionTranslator implements PersistenceExceptionTranslator { | ||
|
||
private static final Set<Class<? extends DataAccessException>> PASS_THROUGH_EXCEPTIONS = Set.of( | ||
OptimisticLockingFailureException.class, | ||
DuplicateKeyException.class | ||
); | ||
|
||
@Override | ||
public DataAccessException translateExceptionIfPossible(RuntimeException ex) { | ||
if (PASS_THROUGH_EXCEPTIONS.contains(ex.getClass())) { | ||
return (DataAccessException) ex; | ||
} | ||
|
||
return null; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ta-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcExceptionTranslatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.springframework.data.jdbc.core; | ||
|
||
import java.util.stream.Stream; | ||
import org.assertj.core.api.Assertions; | ||
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.springframework.dao.DataAccessException; | ||
import org.springframework.dao.DuplicateKeyException; | ||
import org.springframework.dao.OptimisticLockingFailureException; | ||
|
||
/** | ||
* Unit tests for {@link JdbcExceptionTranslator} | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
class JdbcExceptionTranslatorTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource(value = "passThroughExceptionsSource") | ||
void testPassThroughExceptions(DataAccessException exception) { | ||
|
||
// when | ||
DataAccessException translated = new JdbcExceptionTranslator().translateExceptionIfPossible(exception); | ||
|
||
// then. | ||
Assertions.assertThat(translated).isSameAs(exception); | ||
} | ||
|
||
@Test | ||
void testUnrecognizedException() { | ||
|
||
// when | ||
DataAccessException translated = new JdbcExceptionTranslator().translateExceptionIfPossible(new IllegalArgumentException()); | ||
|
||
// then. | ||
Assertions.assertThat(translated).isNull(); | ||
} | ||
|
||
static Stream<Arguments> passThroughExceptionsSource() { | ||
return Stream.of(Arguments.of(new OptimisticLockingFailureException("")), Arguments.of(new DuplicateKeyException(""))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters