-
Notifications
You must be signed in to change notification settings - Fork 132
#57 - Add support for R2DBC subclass exception translation. #97
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
Closed
Closed
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
src/main/java/org/springframework/data/r2dbc/support/R2dbcExceptionSubclassTranslator.java
This file contains hidden or 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,91 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.r2dbc.support; | ||
|
||
import io.r2dbc.spi.R2dbcBadGrammarException; | ||
import io.r2dbc.spi.R2dbcDataIntegrityViolationException; | ||
import io.r2dbc.spi.R2dbcException; | ||
import io.r2dbc.spi.R2dbcNonTransientException; | ||
import io.r2dbc.spi.R2dbcNonTransientResourceException; | ||
import io.r2dbc.spi.R2dbcPermissionDeniedException; | ||
import io.r2dbc.spi.R2dbcRollbackException; | ||
import io.r2dbc.spi.R2dbcTimeoutException; | ||
import io.r2dbc.spi.R2dbcTransientException; | ||
import io.r2dbc.spi.R2dbcTransientResourceException; | ||
|
||
import org.springframework.dao.ConcurrencyFailureException; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.dao.DataAccessResourceFailureException; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.dao.PermissionDeniedDataAccessException; | ||
import org.springframework.dao.QueryTimeoutException; | ||
import org.springframework.dao.TransientDataAccessResourceException; | ||
import org.springframework.data.r2dbc.BadSqlGrammarException; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* {@link R2dbcExceptionTranslator} implementation which analyzes the specific {@link R2dbcException} subclass thrown by | ||
* the R2DBC driver. | ||
* <p> | ||
* Falls back to a standard {@link SqlStateR2dbcExceptionTranslator}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
public class R2dbcExceptionSubclassTranslator extends AbstractFallbackR2dbcExceptionTranslator { | ||
|
||
public R2dbcExceptionSubclassTranslator() { | ||
setFallbackTranslator(new SqlStateR2dbcExceptionTranslator()); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.r2dbc.support.AbstractFallbackR2dbcExceptionTranslator#doTranslate(java.lang.String, java.lang.String, io.r2dbc.spi.R2dbcException) | ||
*/ | ||
@Override | ||
@Nullable | ||
protected DataAccessException doTranslate(String task, @Nullable String sql, R2dbcException ex) { | ||
|
||
if (ex instanceof R2dbcTransientException) { | ||
if (ex instanceof R2dbcTransientResourceException) { | ||
return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex); | ||
} | ||
if (ex instanceof R2dbcRollbackException) { | ||
return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex); | ||
} | ||
if (ex instanceof R2dbcTimeoutException) { | ||
return new QueryTimeoutException(buildMessage(task, sql, ex), ex); | ||
} | ||
} | ||
|
||
if (ex instanceof R2dbcNonTransientException) { | ||
if (ex instanceof R2dbcNonTransientResourceException) { | ||
return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex); | ||
} | ||
if (ex instanceof R2dbcDataIntegrityViolationException) { | ||
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex); | ||
} | ||
if (ex instanceof R2dbcPermissionDeniedException) { | ||
return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex); | ||
} | ||
if (ex instanceof R2dbcBadGrammarException) { | ||
return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex); | ||
} | ||
} | ||
|
||
// Fallback to Spring's own R2DBC state translation... | ||
return null; | ||
} | ||
} |
This file contains hidden or 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
144 changes: 144 additions & 0 deletions
144
...ava/org/springframework/data/r2dbc/support/R2dbcExceptionSubclassTranslatorUnitTests.java
This file contains hidden or 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,144 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.r2dbc.support; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import io.r2dbc.spi.R2dbcBadGrammarException; | ||
import io.r2dbc.spi.R2dbcDataIntegrityViolationException; | ||
import io.r2dbc.spi.R2dbcException; | ||
import io.r2dbc.spi.R2dbcNonTransientResourceException; | ||
import io.r2dbc.spi.R2dbcPermissionDeniedException; | ||
import io.r2dbc.spi.R2dbcRollbackException; | ||
import io.r2dbc.spi.R2dbcTimeoutException; | ||
import io.r2dbc.spi.R2dbcTransientResourceException; | ||
|
||
import org.junit.Test; | ||
import org.springframework.dao.ConcurrencyFailureException; | ||
import org.springframework.dao.DataAccessResourceFailureException; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.dao.PermissionDeniedDataAccessException; | ||
import org.springframework.dao.QueryTimeoutException; | ||
import org.springframework.dao.TransientDataAccessResourceException; | ||
import org.springframework.data.r2dbc.BadSqlGrammarException; | ||
import org.springframework.data.r2dbc.UncategorizedR2dbcException; | ||
|
||
/** | ||
* Unit tests for {@link R2dbcExceptionSubclassTranslator}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
public class R2dbcExceptionSubclassTranslatorUnitTests { | ||
|
||
R2dbcExceptionSubclassTranslator translator = new R2dbcExceptionSubclassTranslator(); | ||
|
||
@Test // gh-57 | ||
public void shouldTranslateTransientResourceException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcTransientResourceException("")); | ||
|
||
assertThat(exception) | ||
.isInstanceOf(TransientDataAccessResourceException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldTranslateRollbackException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcRollbackException()); | ||
|
||
assertThat(exception).isInstanceOf(ConcurrencyFailureException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldTranslateTimeoutException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcTimeoutException()); | ||
|
||
assertThat(exception).isInstanceOf(QueryTimeoutException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldNotTranslateUnknownExceptions() { | ||
|
||
Exception exception = translator.translate("", "", new MyTransientExceptions()); | ||
|
||
assertThat(exception).isInstanceOf(UncategorizedR2dbcException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldTranslateNonTransientResourceException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcNonTransientResourceException()); | ||
|
||
assertThat(exception).isInstanceOf(DataAccessResourceFailureException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldTranslateIntegrityViolationException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcDataIntegrityViolationException()); | ||
|
||
assertThat(exception).isInstanceOf(DataIntegrityViolationException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldTranslatePermissionDeniedException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcPermissionDeniedException()); | ||
|
||
assertThat(exception).isInstanceOf(PermissionDeniedDataAccessException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void shouldTranslateBadSqlGrammarException() { | ||
|
||
Exception exception = translator.translate("", "", new R2dbcBadGrammarException()); | ||
|
||
assertThat(exception).isInstanceOf(BadSqlGrammarException.class); | ||
} | ||
|
||
@Test // gh-57 | ||
public void messageGeneration() { | ||
|
||
Exception exception = translator.translate("TASK", "SOME-SQL", new R2dbcTransientResourceException("MESSAGE")); | ||
|
||
assertThat(exception) // | ||
.isInstanceOf(TransientDataAccessResourceException.class) // | ||
.hasMessage("TASK; SQL [SOME-SQL]; MESSAGE; nested exception is io.r2dbc.spi.R2dbcTransientResourceException: MESSAGE"); | ||
} | ||
|
||
@Test // gh-57 | ||
public void messageGenerationNullSQL() { | ||
|
||
Exception exception = translator.translate("TASK", null, new R2dbcTransientResourceException("MESSAGE")); | ||
|
||
assertThat(exception) // | ||
.isInstanceOf(TransientDataAccessResourceException.class) // | ||
.hasMessage("TASK; MESSAGE; nested exception is io.r2dbc.spi.R2dbcTransientResourceException: MESSAGE"); | ||
} | ||
|
||
@Test // gh-57 | ||
public void messageGenerationNullMessage() { | ||
|
||
Exception exception = translator.translate("TASK", "SOME-SQL", new R2dbcTransientResourceException()); | ||
|
||
assertThat(exception) // | ||
.isInstanceOf(TransientDataAccessResourceException.class) // | ||
.hasMessage("TASK; SQL [SOME-SQL]; null; nested exception is io.r2dbc.spi.R2dbcTransientResourceException"); | ||
} | ||
|
||
private static class MyTransientExceptions extends R2dbcException {} | ||
} |
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.
What are these outer
if
statements for?Performance?
Is this worth it?
I find it rather confusing and given that exceptions should be the exception, saving up to two conditionals by paying 1-2 extra doesn't seem worth it.
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.
This is a branch organization aspect that maintains readability. Transient resources come in one block, non-transients in the other one. See also https://github.com/spring-projects/spring-framework/blob/master/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslator.java.