Skip to content

Commit 18e509e

Browse files
Blackneysobychacko
authored andcommitted
GH-3227: Implement handleOne() in CDEH
Fixes: #3227 * Implement handleOne() in `CommonDelegatingErrorHandler` * Add tests for handle methods in `CommonDelegatingErrorHandler` * Add tests for the new `handleOne()` method, as well as a test for `handleOtherException()` * Checkstyle fixes (cherry picked from commit 4e06c2c)
1 parent 4a727e1 commit 18e509e

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/CommonDelegatingErrorHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*
3939
* @author Gary Russell
4040
* @author Adrian Chlebosz
41+
* @author Antonin Arquey
42+
* @author Dan Blackney
4143
* @since 2.8
4244
*
4345
*/
@@ -188,6 +190,19 @@ public void handleOtherException(Exception thrownException, Consumer<?, ?> consu
188190
}
189191
}
190192

193+
@Override
194+
public boolean handleOne(Exception thrownException, ConsumerRecord<?, ?> record, Consumer<?, ?> consumer,
195+
MessageListenerContainer container) {
196+
197+
CommonErrorHandler handler = findDelegate(thrownException);
198+
if (handler != null) {
199+
return handler.handleOne(thrownException, record, consumer, container);
200+
}
201+
else {
202+
return this.defaultErrorHandler.handleOne(thrownException, record, consumer, container);
203+
}
204+
}
205+
191206
@Nullable
192207
private CommonErrorHandler findDelegate(Throwable thrownException) {
193208
Throwable cause = findCause(thrownException);

spring-kafka/src/test/java/org/springframework/kafka/listener/CommonDelegatingErrorHandlerTests.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyBoolean;
2122
import static org.mockito.Mockito.mock;
2223
import static org.mockito.Mockito.never;
2324
import static org.mockito.Mockito.verify;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829

2930
import org.apache.kafka.clients.consumer.Consumer;
31+
import org.apache.kafka.clients.consumer.ConsumerRecord;
3032
import org.apache.kafka.clients.consumer.ConsumerRecords;
3133
import org.junit.jupiter.api.Test;
3234

@@ -39,13 +41,15 @@
3941
*
4042
* @author Gary Russell
4143
* @author Adrian Chlebosz
44+
* @author Antonin Arquey
45+
* @author Dan Blackney
4246
* @since 2.8
4347
*
4448
*/
4549
public class CommonDelegatingErrorHandlerTests {
4650

4751
@Test
48-
void testRecordDelegates() {
52+
void testHandleRemainingDelegates() {
4953
var def = mock(CommonErrorHandler.class);
5054
var one = mock(CommonErrorHandler.class);
5155
var two = mock(CommonErrorHandler.class);
@@ -69,7 +73,7 @@ void testRecordDelegates() {
6973
}
7074

7175
@Test
72-
void testBatchDelegates() {
76+
void testHandleBatchDelegates() {
7377
var def = mock(CommonErrorHandler.class);
7478
var one = mock(CommonErrorHandler.class);
7579
var two = mock(CommonErrorHandler.class);
@@ -92,6 +96,54 @@ void testBatchDelegates() {
9296
verify(one).handleBatch(any(), any(), any(), any(), any());
9397
}
9498

99+
@Test
100+
void testHandleOtherExceptionDelegates() {
101+
var def = mock(CommonErrorHandler.class);
102+
var one = mock(CommonErrorHandler.class);
103+
var two = mock(CommonErrorHandler.class);
104+
var three = mock(CommonErrorHandler.class);
105+
var eh = new CommonDelegatingErrorHandler(def);
106+
eh.setErrorHandlers(Map.of(IllegalStateException.class, one, IllegalArgumentException.class, two));
107+
eh.addDelegate(RuntimeException.class, three);
108+
109+
eh.handleOtherException(wrap(new IOException()), mock(Consumer.class),
110+
mock(MessageListenerContainer.class), true);
111+
verify(def).handleOtherException(any(), any(), any(), anyBoolean());
112+
eh.handleOtherException(wrap(new KafkaException("test")), mock(Consumer.class),
113+
mock(MessageListenerContainer.class), true);
114+
verify(three).handleOtherException(any(), any(), any(), anyBoolean());
115+
eh.handleOtherException(wrap(new IllegalArgumentException()), mock(Consumer.class),
116+
mock(MessageListenerContainer.class), true);
117+
verify(two).handleOtherException(any(), any(), any(), anyBoolean());
118+
eh.handleOtherException(wrap(new IllegalStateException()), mock(Consumer.class),
119+
mock(MessageListenerContainer.class), true);
120+
verify(one).handleOtherException(any(), any(), any(), anyBoolean());
121+
}
122+
123+
@Test
124+
void testHandleOneDelegates() {
125+
var def = mock(CommonErrorHandler.class);
126+
var one = mock(CommonErrorHandler.class);
127+
var two = mock(CommonErrorHandler.class);
128+
var three = mock(CommonErrorHandler.class);
129+
var eh = new CommonDelegatingErrorHandler(def);
130+
eh.setErrorHandlers(Map.of(IllegalStateException.class, one, IllegalArgumentException.class, two));
131+
eh.addDelegate(RuntimeException.class, three);
132+
133+
eh.handleOne(wrap(new IOException()), mock(ConsumerRecord.class), mock(Consumer.class),
134+
mock(MessageListenerContainer.class));
135+
verify(def).handleOne(any(), any(), any(), any());
136+
eh.handleOne(wrap(new KafkaException("test")), mock(ConsumerRecord.class), mock(Consumer.class),
137+
mock(MessageListenerContainer.class));
138+
verify(three).handleOne(any(), any(), any(), any());
139+
eh.handleOne(wrap(new IllegalArgumentException()), mock(ConsumerRecord.class), mock(Consumer.class),
140+
mock(MessageListenerContainer.class));
141+
verify(two).handleOne(any(), any(), any(), any());
142+
eh.handleOne(wrap(new IllegalStateException()), mock(ConsumerRecord.class), mock(Consumer.class),
143+
mock(MessageListenerContainer.class));
144+
verify(one).handleOne(any(), any(), any(), any());
145+
}
146+
95147
@Test
96148
void testDelegateForThrowableIsAppliedWhenCauseTraversingIsEnabled() {
97149
var defaultHandler = mock(CommonErrorHandler.class);

0 commit comments

Comments
 (0)