Skip to content

Commit 758a2ee

Browse files
committed
Don't pass null exception translation result into jOOQ
Closes gh-25717
1 parent c815195 commit 758a2ee

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,10 +80,12 @@ private SQLExceptionTranslator getTranslator(ExecuteContext context) {
8080
private void handle(ExecuteContext context, SQLExceptionTranslator translator, SQLException exception) {
8181
DataAccessException translated = translate(context, translator, exception);
8282
if (exception.getNextException() == null) {
83-
context.exception(translated);
83+
if (translated != null) {
84+
context.exception(translated);
85+
}
8486
}
8587
else {
86-
logger.error("Execution of SQL statement failed.", translated);
88+
logger.error("Execution of SQL statement failed.", (translated != null) ? translated : exception);
8789
}
8890
}
8991

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,15 +21,18 @@
2121
import org.jooq.Configuration;
2222
import org.jooq.ExecuteContext;
2323
import org.jooq.SQLDialect;
24+
import org.junit.jupiter.api.Test;
2425
import org.junit.jupiter.params.ParameterizedTest;
2526
import org.junit.jupiter.params.provider.MethodSource;
2627
import org.mockito.ArgumentCaptor;
2728

2829
import org.springframework.jdbc.BadSqlGrammarException;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.ArgumentMatchers.any;
3133
import static org.mockito.BDDMockito.given;
3234
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.times;
3336
import static org.mockito.Mockito.verify;
3437

3538
/**
@@ -55,6 +58,17 @@ void exceptionTranslation(SQLDialect dialect, SQLException sqlException) {
5558
assertThat(captor.getValue()).isInstanceOf(BadSqlGrammarException.class);
5659
}
5760

61+
@Test
62+
void whenExceptionCannotBeTranslatedThenExecuteContextExceptionIsNotCalled() {
63+
ExecuteContext context = mock(ExecuteContext.class);
64+
Configuration configuration = mock(Configuration.class);
65+
given(context.configuration()).willReturn(configuration);
66+
given(configuration.dialect()).willReturn(SQLDialect.POSTGRES);
67+
given(context.sqlException()).willReturn(new SQLException(null, null, 123456789));
68+
this.exceptionTranslator.exception(context);
69+
verify(context, times(0)).exception(any());
70+
}
71+
5872
static Object[] exceptionTranslation() {
5973
return new Object[] { new Object[] { SQLDialect.DERBY, sqlException("42802") },
6074
new Object[] { SQLDialect.H2, sqlException(42000) },

0 commit comments

Comments
 (0)