|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.r2dbc.support; |
| 17 | + |
| 18 | +import io.r2dbc.spi.R2dbcBadGrammarException; |
| 19 | +import io.r2dbc.spi.R2dbcDataIntegrityViolationException; |
| 20 | +import io.r2dbc.spi.R2dbcException; |
| 21 | +import io.r2dbc.spi.R2dbcNonTransientException; |
| 22 | +import io.r2dbc.spi.R2dbcNonTransientResourceException; |
| 23 | +import io.r2dbc.spi.R2dbcPermissionDeniedException; |
| 24 | +import io.r2dbc.spi.R2dbcRollbackException; |
| 25 | +import io.r2dbc.spi.R2dbcTimeoutException; |
| 26 | +import io.r2dbc.spi.R2dbcTransientException; |
| 27 | +import io.r2dbc.spi.R2dbcTransientResourceException; |
| 28 | + |
| 29 | +import org.springframework.dao.ConcurrencyFailureException; |
| 30 | +import org.springframework.dao.DataAccessException; |
| 31 | +import org.springframework.dao.DataAccessResourceFailureException; |
| 32 | +import org.springframework.dao.DataIntegrityViolationException; |
| 33 | +import org.springframework.dao.PermissionDeniedDataAccessException; |
| 34 | +import org.springframework.dao.QueryTimeoutException; |
| 35 | +import org.springframework.dao.TransientDataAccessResourceException; |
| 36 | +import org.springframework.data.r2dbc.BadSqlGrammarException; |
| 37 | +import org.springframework.lang.Nullable; |
| 38 | + |
| 39 | +/** |
| 40 | + * {@link R2dbcExceptionTranslator} implementation which analyzes the specific {@link R2dbcException} subclass thrown by |
| 41 | + * the R2DBC driver. |
| 42 | + * <p> |
| 43 | + * Falls back to a standard {@link SqlStateR2dbcExceptionTranslator}. |
| 44 | + * |
| 45 | + * @author Mark Paluch |
| 46 | + */ |
| 47 | +public class R2dbcExceptionSubclassTranslator extends AbstractFallbackR2dbcExceptionTranslator { |
| 48 | + |
| 49 | + public R2dbcExceptionSubclassTranslator() { |
| 50 | + setFallbackTranslator(new SqlStateR2dbcExceptionTranslator()); |
| 51 | + } |
| 52 | + |
| 53 | + /* |
| 54 | + * (non-Javadoc) |
| 55 | + * @see org.springframework.data.r2dbc.support.AbstractFallbackR2dbcExceptionTranslator#doTranslate(java.lang.String, java.lang.String, io.r2dbc.spi.R2dbcException) |
| 56 | + */ |
| 57 | + @Override |
| 58 | + @Nullable |
| 59 | + protected DataAccessException doTranslate(String task, @Nullable String sql, R2dbcException ex) { |
| 60 | + |
| 61 | + if (ex instanceof R2dbcTransientException) { |
| 62 | + if (ex instanceof R2dbcTransientResourceException) { |
| 63 | + return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex); |
| 64 | + } |
| 65 | + if (ex instanceof R2dbcRollbackException) { |
| 66 | + return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex); |
| 67 | + } |
| 68 | + if (ex instanceof R2dbcTimeoutException) { |
| 69 | + return new QueryTimeoutException(buildMessage(task, sql, ex), ex); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (ex instanceof R2dbcNonTransientException) { |
| 74 | + if (ex instanceof R2dbcNonTransientResourceException) { |
| 75 | + return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex); |
| 76 | + } |
| 77 | + if (ex instanceof R2dbcDataIntegrityViolationException) { |
| 78 | + return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex); |
| 79 | + } |
| 80 | + if (ex instanceof R2dbcPermissionDeniedException) { |
| 81 | + return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex); |
| 82 | + } |
| 83 | + if (ex instanceof R2dbcBadGrammarException) { |
| 84 | + return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Fallback to Spring's own R2DBC state translation... |
| 89 | + return null; |
| 90 | + } |
| 91 | +} |
0 commit comments