Skip to content

Commit 0213268

Browse files
garyrussellartembilan
authored andcommitted
GH-2306: Improve DeserEx Message For Improper Ex.
Resolves #2306 Include the original exception message in the `DeserializationException.cause` when the original exception could not be serialized. **cherry-pick to 2.9.x, 2.8.x**
1 parent 960d6a4 commit 0213268

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

spring-kafka/src/main/java/org/springframework/kafka/support/serializer/SerializationUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 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.
@@ -156,9 +156,9 @@ public static void deserializationException(Headers headers, byte[] data, Except
156156
stream = new ByteArrayOutputStream();
157157
try (ObjectOutputStream oos = new ObjectOutputStream(stream)) {
158158
exception = new DeserializationException("failed to deserialize",
159-
data, isForKeyArg, new RuntimeException("Could not deserialize type "
160-
+ ioex.getClass().getName() + " with message " + ioex.getMessage()
161-
+ " failure: " + ioex.getMessage()));
159+
data, isForKeyArg, new RuntimeException("Could not serialize type "
160+
+ ex.getClass().getName() + " with message " + ioex.getMessage()
161+
+ ". Original exception message: " + ex.getMessage()));
162162
oos.writeObject(exception);
163163
}
164164
catch (IOException ex2) {

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ public void close() {
110110
ehd.close();
111111
}
112112

113+
@Test
114+
void notSerializable() {
115+
class MyDes implements Deserializer<String> {
116+
117+
@Override
118+
public String deserialize(String topic, byte[] data) {
119+
return null;
120+
}
121+
122+
@Override
123+
public String deserialize(String topic, Headers headers, byte[] data) {
124+
throw new CannotSerializeException("original exception message");
125+
}
126+
127+
}
128+
ErrorHandlingDeserializer<String> ehd = new ErrorHandlingDeserializer<>(new MyDes());
129+
Headers headers = new RecordHeaders();
130+
ehd.deserialize("foo", headers, new byte[1]);
131+
DeserializationException dex = ListenerUtils.byteArrayToDeserializationException(null,
132+
headers.lastHeader(SerializationUtils.VALUE_DESERIALIZER_EXCEPTION_HEADER).value());
133+
assertThat(dex.getMessage())
134+
.contains("Could not serialize")
135+
.contains("original exception message");
136+
}
137+
113138
@Configuration
114139
@EnableKafka
115140
public static class Config {
@@ -237,4 +262,19 @@ public static class ExtendedEHD<T> extends ErrorHandlingDeserializer<T> {
237262

238263
}
239264

265+
@SuppressWarnings("serial")
266+
public static class CannotSerializeException extends RuntimeException {
267+
268+
private final Foo foo = new Foo();
269+
270+
public CannotSerializeException(String message) {
271+
super(message);
272+
}
273+
274+
}
275+
276+
public static class Foo {
277+
278+
}
279+
240280
}

0 commit comments

Comments
 (0)