-
Notifications
You must be signed in to change notification settings - Fork 355
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
GH-611 added JdbcExceptionTranslator #1956
Open
mipo256
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
mipo256:GH-611
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that we should revisit our exception handling.
DbActionExecutionException
doesn't seem particularly useful to the final application whereas it might be useful for our own internals.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mp911de Yeah... But this is not a part of this issue. Maybe we can create another issue to revise the default exception to be thrown? Or what should we do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that we're throwing
DbActionExecutionException
and not a DAO exception, so I think that is indeed part of the issue. Looking at the code, we do not catchDbActionExecutionException
. That exception was introduced as part of introducing better means for debugging (#382) while the net effect was that we changed the API experience. What we could do is addDbActionExecutionException
as suppressed exception to the original one.Paging @schauder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by "adding as suppresed exception".
In my understanding Surpressed exceptions are ones that get lost when a finalizer throws an exception itself.
We could instantiate a new exception with the same type as the original with the additional information of the DAEE, or even the DAEE as cause. That would certainly look strange, but might actually be rather useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something along the lines of:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little bit confused with adding suppressed exception here. We're not really suppressing
DbActionExecutionException
here. I see it confusing to the end user TBH.CC: @mp911de @schauder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know
addSuppressed
. It is a little hackish, but seems fine to me. It's not worse then the other variants I had in mind.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done