From e93e49f268ce93ba67d33e71a79f05dc3a195ba9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 19 Apr 2017 20:50:20 +0200 Subject: [PATCH] ErrorMessage.toString() includes original message at top level Issue: SPR-15459 --- .../messaging/support/ErrorMessage.java | 38 +++++++++---------- .../messaging/support/ErrorMessageTests.java | 9 +++-- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/ErrorMessage.java b/spring-messaging/src/main/java/org/springframework/messaging/support/ErrorMessage.java index 61dfcd002b..402ee02966 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/ErrorMessage.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/ErrorMessage.java @@ -23,10 +23,12 @@ import org.springframework.messaging.MessageHeaders; /** * A {@link GenericMessage} with a {@link Throwable} payload. - * The payload is typically a {@link org.springframework.messaging.MessagingException} + * + *

The payload is typically a {@link org.springframework.messaging.MessagingException} * with the message at the point of failure in its {@code failedMessage} property. * An optional {@code originalMessage} may be provided, which represents the message * that existed at the point in the stack where the error message is created. + * *

Consider some code that starts with a message, invokes some process that performs * transformation on that message and then fails for some reason, throwing the exception. * The exception is caught and an error message produced that contains both the original @@ -44,6 +46,7 @@ public class ErrorMessage extends GenericMessage { private final Message originalMessage; + /** * Create a new message with the given payload. * @param payload the message payload (never {@code null}) @@ -79,8 +82,8 @@ public class ErrorMessage extends GenericMessage { /** * Create a new message with the given payload and original message. * @param payload the message payload (never {@code null}) - * @param originalMessage the original message (if present) at the point in the stack - * where the ErrorMessage was created + * @param originalMessage the original message (if present) at the point + * in the stack where the ErrorMessage was created * @since 5.0 */ public ErrorMessage(Throwable payload, Message originalMessage) { @@ -93,8 +96,8 @@ public class ErrorMessage extends GenericMessage { * The content of the given header map is copied. * @param payload the message payload (never {@code null}) * @param headers message headers to use for initialization - * @param originalMessage the original message (if present) at the point in the stack - * where the ErrorMessage was created + * @param originalMessage the original message (if present) at the point + * in the stack where the ErrorMessage was created * @since 5.0 */ public ErrorMessage(Throwable payload, Map headers, Message originalMessage) { @@ -108,8 +111,8 @@ public class ErrorMessage extends GenericMessage { * is used directly in the new message, i.e. it is not copied. * @param payload the message payload (never {@code null}) * @param headers message headers - * @param originalMessage the original message (if present) at the point in the stack - * where the ErrorMessage was created + * @param originalMessage the original message (if present) at the point + * in the stack where the ErrorMessage was created * @since 5.0 */ public ErrorMessage(Throwable payload, MessageHeaders headers, Message originalMessage) { @@ -117,13 +120,14 @@ public class ErrorMessage extends GenericMessage { this.originalMessage = originalMessage; } + /** - * The original message (if present) at the point in the stack where the - * ErrorMessage was created. - * @return the originalMessage + * Return the original message (if available) at the point in the stack + * where the ErrorMessage was created. + * @since 5.0 */ public Message getOriginalMessage() { - return originalMessage; + return this.originalMessage; } @Override @@ -131,14 +135,10 @@ public class ErrorMessage extends GenericMessage { if (this.originalMessage == null) { return super.toString(); } - else { - StringBuilder sb = new StringBuilder(super.toString()); - if (sb.length() > 0) { - sb.setLength(sb.length() - 1); - } - sb.append(", originalMessage=").append(this.originalMessage.toString()).append("]"); - return sb.toString(); - } + + StringBuilder sb = new StringBuilder(super.toString()); + sb.append(" for original ").append(this.originalMessage); + return sb.toString(); } } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/support/ErrorMessageTests.java b/spring-messaging/src/test/java/org/springframework/messaging/support/ErrorMessageTests.java index ce617c7051..b8ca0cfd97 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/support/ErrorMessageTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/support/ErrorMessageTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; /** - * * @author Gary Russell * @since 5.0 */ @@ -32,10 +31,12 @@ public class ErrorMessageTests { public void testToString() { ErrorMessage em = new ErrorMessage(new RuntimeException("foo")); String emString = em.toString(); - assertThat(emString, not(containsString("originalMessage"))); + assertThat(emString, not(containsString("original"))); + em = new ErrorMessage(new RuntimeException("foo"), new GenericMessage<>("bar")); emString = em.toString(); - assertThat(emString, containsString("}, originalMessage=")); + assertThat(emString, containsString("original")); + assertThat(emString, containsString(em.getOriginalMessage().toString())); } }