Skip to content

Commit e93e49f

Browse files
committed
ErrorMessage.toString() includes original message at top level
Issue: SPR-15459
1 parent c16c8f2 commit e93e49f

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

spring-messaging/src/main/java/org/springframework/messaging/support/ErrorMessage.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323

2424
/**
2525
* A {@link GenericMessage} with a {@link Throwable} payload.
26-
* The payload is typically a {@link org.springframework.messaging.MessagingException}
26+
*
27+
* <p>The payload is typically a {@link org.springframework.messaging.MessagingException}
2728
* with the message at the point of failure in its {@code failedMessage} property.
2829
* An optional {@code originalMessage} may be provided, which represents the message
2930
* that existed at the point in the stack where the error message is created.
31+
*
3032
* <p>Consider some code that starts with a message, invokes some process that performs
3133
* transformation on that message and then fails for some reason, throwing the exception.
3234
* The exception is caught and an error message produced that contains both the original
@@ -44,6 +46,7 @@ public class ErrorMessage extends GenericMessage<Throwable> {
4446

4547
private final Message<?> originalMessage;
4648

49+
4750
/**
4851
* Create a new message with the given payload.
4952
* @param payload the message payload (never {@code null})
@@ -79,8 +82,8 @@ public ErrorMessage(Throwable payload, MessageHeaders headers) {
7982
/**
8083
* Create a new message with the given payload and original message.
8184
* @param payload the message payload (never {@code null})
82-
* @param originalMessage the original message (if present) at the point in the stack
83-
* where the ErrorMessage was created
85+
* @param originalMessage the original message (if present) at the point
86+
* in the stack where the ErrorMessage was created
8487
* @since 5.0
8588
*/
8689
public ErrorMessage(Throwable payload, Message<?> originalMessage) {
@@ -93,8 +96,8 @@ public ErrorMessage(Throwable payload, Message<?> originalMessage) {
9396
* The content of the given header map is copied.
9497
* @param payload the message payload (never {@code null})
9598
* @param headers message headers to use for initialization
96-
* @param originalMessage the original message (if present) at the point in the stack
97-
* where the ErrorMessage was created
99+
* @param originalMessage the original message (if present) at the point
100+
* in the stack where the ErrorMessage was created
98101
* @since 5.0
99102
*/
100103
public ErrorMessage(Throwable payload, Map<String, Object> headers, Message<?> originalMessage) {
@@ -108,37 +111,34 @@ public ErrorMessage(Throwable payload, Map<String, Object> headers, Message<?> o
108111
* is used directly in the new message, i.e. it is not copied.
109112
* @param payload the message payload (never {@code null})
110113
* @param headers message headers
111-
* @param originalMessage the original message (if present) at the point in the stack
112-
* where the ErrorMessage was created
114+
* @param originalMessage the original message (if present) at the point
115+
* in the stack where the ErrorMessage was created
113116
* @since 5.0
114117
*/
115118
public ErrorMessage(Throwable payload, MessageHeaders headers, Message<?> originalMessage) {
116119
super(payload, headers);
117120
this.originalMessage = originalMessage;
118121
}
119122

123+
120124
/**
121-
* The original message (if present) at the point in the stack where the
122-
* ErrorMessage was created.
123-
* @return the originalMessage
125+
* Return the original message (if available) at the point in the stack
126+
* where the ErrorMessage was created.
127+
* @since 5.0
124128
*/
125129
public Message<?> getOriginalMessage() {
126-
return originalMessage;
130+
return this.originalMessage;
127131
}
128132

129133
@Override
130134
public String toString() {
131135
if (this.originalMessage == null) {
132136
return super.toString();
133137
}
134-
else {
135-
StringBuilder sb = new StringBuilder(super.toString());
136-
if (sb.length() > 0) {
137-
sb.setLength(sb.length() - 1);
138-
}
139-
sb.append(", originalMessage=").append(this.originalMessage.toString()).append("]");
140-
return sb.toString();
141-
}
138+
139+
StringBuilder sb = new StringBuilder(super.toString());
140+
sb.append(" for original ").append(this.originalMessage);
141+
return sb.toString();
142142
}
143143

144144
}

spring-messaging/src/test/java/org/springframework/messaging/support/ErrorMessageTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -22,7 +22,6 @@
2222
import static org.junit.Assert.*;
2323

2424
/**
25-
*
2625
* @author Gary Russell
2726
* @since 5.0
2827
*/
@@ -32,10 +31,12 @@ public class ErrorMessageTests {
3231
public void testToString() {
3332
ErrorMessage em = new ErrorMessage(new RuntimeException("foo"));
3433
String emString = em.toString();
35-
assertThat(emString, not(containsString("originalMessage")));
34+
assertThat(emString, not(containsString("original")));
35+
3636
em = new ErrorMessage(new RuntimeException("foo"), new GenericMessage<>("bar"));
3737
emString = em.toString();
38-
assertThat(emString, containsString("}, originalMessage="));
38+
assertThat(emString, containsString("original"));
39+
assertThat(emString, containsString(em.getOriginalMessage().toString()));
3940
}
4041

4142
}

0 commit comments

Comments
 (0)