Skip to content

Commit 7be4de6

Browse files
committed
spring-projectsGH-2306: Improve DeserEx Message For Improper Ex.
Resolves spring-projects#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 a5a28cc commit 7be4de6

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ 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()).contains("original exception message");
135+
}
136+
114137
@Configuration
115138
@EnableKafka
116139
public static class Config {
@@ -245,4 +268,19 @@ public static class ExtendedEHD<T> extends ErrorHandlingDeserializer<T> {
245268

246269
}
247270

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

0 commit comments

Comments
 (0)