Skip to content

Commit 8ea39c4

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 a07ace9 commit 8ea39c4

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

+4-4
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) {

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

+40
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ public void close() {
111111
ehd.close();
112112
}
113113

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

246271
}
247272

273+
@SuppressWarnings("serial")
274+
public static class CannotSerializeException extends RuntimeException {
275+
276+
private final Foo foo = new Foo();
277+
278+
public CannotSerializeException(String message) {
279+
super(message);
280+
}
281+
282+
}
283+
284+
public static class Foo {
285+
286+
}
287+
248288
}

0 commit comments

Comments
 (0)