Skip to content

Commit 30dca49

Browse files
garyrussellartembilan
authored andcommitted
GH-1287: Detect mis-configured deserialization
Resolves #1287 Throw an `IllegalStateException` in the `SeekToCurrentErrorHandler` if the root exception is a `SerializationException`. Handling of deserializatin problms requires the configuration of an `ErrorHandlingDeserializer2`. **cherry-pick to 2.2.x, 2.1.x**
1 parent 78a0f15 commit 30dca49

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
2727
import org.apache.kafka.clients.consumer.OffsetCommitCallback;
2828
import org.apache.kafka.common.TopicPartition;
29+
import org.apache.kafka.common.errors.SerializationException;
2930

3031
import org.springframework.classify.BinaryExceptionClassifier;
3132
import org.springframework.kafka.KafkaException;
@@ -180,6 +181,12 @@ public void setClassifier(BinaryExceptionClassifier classifier) {
180181
public void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records,
181182
Consumer<?, ?> consumer, MessageListenerContainer container) {
182183

184+
if (thrownException instanceof SerializationException) {
185+
throw new IllegalStateException("This error handler cannot process 'SerializationException's directly, "
186+
+ "please consider configuring an 'ErrorHandlingDeserializer2' in the value and/or key "
187+
+ "deserializer", thrownException);
188+
}
189+
183190
if (!SeekUtils.doSeeks(records, consumer, thrownException, true, getSkipPredicate(records, thrownException),
184191
LOGGER)) {
185192
throw new KafkaException("Seek to current after exception", thrownException);

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

Lines changed: 11 additions & 0 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.assertj.core.api.Assertions.assertThatExceptionOfType;
21+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2122
import static org.mockito.Mockito.inOrder;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.times;
@@ -30,6 +31,7 @@
3031
import org.apache.kafka.clients.consumer.Consumer;
3132
import org.apache.kafka.clients.consumer.ConsumerRecord;
3233
import org.apache.kafka.common.TopicPartition;
34+
import org.apache.kafka.common.errors.SerializationException;
3335
import org.junit.jupiter.api.Test;
3436
import org.mockito.InOrder;
3537

@@ -93,4 +95,13 @@ public void testDeprecatedCtor() {
9395
assertThat(KafkaTestUtils.getPropertyValue(handler, "failureTracker.backOff.maxAttempts"))
9496
.isEqualTo(9L);
9597
}
98+
99+
@Test
100+
void testSerializationException() {
101+
SeekToCurrentErrorHandler handler = new SeekToCurrentErrorHandler();
102+
SerializationException thrownException = new SerializationException();
103+
assertThatIllegalStateException().isThrownBy(() -> handler.handle(thrownException, null, null, null))
104+
.withCause(thrownException);
105+
}
106+
96107
}

0 commit comments

Comments
 (0)