From 758a2ee7ca5b75f289d9e5b64a545fc3578084f2 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 17 Mar 2021 12:25:50 +0000 Subject: [PATCH] Don't pass null exception translation result into jOOQ Closes gh-25717 --- .../jooq/JooqExceptionTranslator.java | 8 +++++--- .../jooq/JooqExceptionTranslatorTests.java | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java index 075934c882b..5a5c4743cd7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -80,10 +80,12 @@ public class JooqExceptionTranslator extends DefaultExecuteListener { private void handle(ExecuteContext context, SQLExceptionTranslator translator, SQLException exception) { DataAccessException translated = translate(context, translator, exception); if (exception.getNextException() == null) { - context.exception(translated); + if (translated != null) { + context.exception(translated); + } } else { - logger.error("Execution of SQL statement failed.", translated); + logger.error("Execution of SQL statement failed.", (translated != null) ? translated : exception); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java index ea88b1c7efb..d405bb7f0b7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -21,6 +21,7 @@ import java.sql.SQLException; import org.jooq.Configuration; import org.jooq.ExecuteContext; import org.jooq.SQLDialect; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.ArgumentCaptor; @@ -28,8 +29,10 @@ import org.mockito.ArgumentCaptor; import org.springframework.jdbc.BadSqlGrammarException; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -55,6 +58,17 @@ class JooqExceptionTranslatorTests { assertThat(captor.getValue()).isInstanceOf(BadSqlGrammarException.class); } + @Test + void whenExceptionCannotBeTranslatedThenExecuteContextExceptionIsNotCalled() { + ExecuteContext context = mock(ExecuteContext.class); + Configuration configuration = mock(Configuration.class); + given(context.configuration()).willReturn(configuration); + given(configuration.dialect()).willReturn(SQLDialect.POSTGRES); + given(context.sqlException()).willReturn(new SQLException(null, null, 123456789)); + this.exceptionTranslator.exception(context); + verify(context, times(0)).exception(any()); + } + static Object[] exceptionTranslation() { return new Object[] { new Object[] { SQLDialect.DERBY, sqlException("42802") }, new Object[] { SQLDialect.H2, sqlException(42000) },