001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.messaging.support; 018 019import java.util.Map; 020 021import org.springframework.lang.Nullable; 022import org.springframework.messaging.Message; 023import org.springframework.messaging.MessageHeaders; 024 025/** 026 * A {@link GenericMessage} with a {@link Throwable} payload. 027 * 028 * <p>The payload is typically a {@link org.springframework.messaging.MessagingException} 029 * with the message at the point of failure in its {@code failedMessage} property. 030 * An optional {@code originalMessage} may be provided, which represents the message 031 * that existed at the point in the stack where the error message is created. 032 * 033 * <p>Consider some code that starts with a message, invokes some process that performs 034 * transformation on that message and then fails for some reason, throwing the exception. 035 * The exception is caught and an error message produced that contains both the original 036 * message, and the transformed message that failed. 037 * 038 * @author Mark Fisher 039 * @author Oleg Zhurakousky 040 * @author Gary Russell 041 * @since 4.0 042 * @see MessageBuilder 043 */ 044public class ErrorMessage extends GenericMessage<Throwable> { 045 046 private static final long serialVersionUID = -5470210965279837728L; 047 048 @Nullable 049 private final Message<?> originalMessage; 050 051 052 /** 053 * Create a new message with the given payload. 054 * @param payload the message payload (never {@code null}) 055 */ 056 public ErrorMessage(Throwable payload) { 057 super(payload); 058 this.originalMessage = null; 059 } 060 061 /** 062 * Create a new message with the given payload and headers. 063 * The content of the given header map is copied. 064 * @param payload the message payload (never {@code null}) 065 * @param headers message headers to use for initialization 066 */ 067 public ErrorMessage(Throwable payload, Map<String, Object> headers) { 068 super(payload, headers); 069 this.originalMessage = null; 070 } 071 072 /** 073 * A constructor with the {@link MessageHeaders} instance to use. 074 * <p><strong>Note:</strong> the given {@code MessageHeaders} instance 075 * is used directly in the new message, i.e. it is not copied. 076 * @param payload the message payload (never {@code null}) 077 * @param headers message headers 078 */ 079 public ErrorMessage(Throwable payload, MessageHeaders headers) { 080 super(payload, headers); 081 this.originalMessage = null; 082 } 083 084 /** 085 * Create a new message with the given payload and original message. 086 * @param payload the message payload (never {@code null}) 087 * @param originalMessage the original message (if present) at the point 088 * in the stack where the ErrorMessage was created 089 * @since 5.0 090 */ 091 public ErrorMessage(Throwable payload, Message<?> originalMessage) { 092 super(payload); 093 this.originalMessage = originalMessage; 094 } 095 096 /** 097 * Create a new message with the given payload, headers and original message. 098 * The content of the given header map is copied. 099 * @param payload the message payload (never {@code null}) 100 * @param headers message headers to use for initialization 101 * @param originalMessage the original message (if present) at the point 102 * in the stack where the ErrorMessage was created 103 * @since 5.0 104 */ 105 public ErrorMessage(Throwable payload, Map<String, Object> headers, Message<?> originalMessage) { 106 super(payload, headers); 107 this.originalMessage = originalMessage; 108 } 109 110 /** 111 * Create a new message with the payload, {@link MessageHeaders} and original message. 112 * <p><strong>Note:</strong> the given {@code MessageHeaders} instance 113 * is used directly in the new message, i.e. it is not copied. 114 * @param payload the message payload (never {@code null}) 115 * @param headers message headers 116 * @param originalMessage the original message (if present) at the point 117 * in the stack where the ErrorMessage was created 118 * @since 5.0 119 */ 120 public ErrorMessage(Throwable payload, MessageHeaders headers, Message<?> originalMessage) { 121 super(payload, headers); 122 this.originalMessage = originalMessage; 123 } 124 125 126 /** 127 * Return the original message (if available) at the point in the stack 128 * where the ErrorMessage was created. 129 * @since 5.0 130 */ 131 @Nullable 132 public Message<?> getOriginalMessage() { 133 return this.originalMessage; 134 } 135 136 @Override 137 public String toString() { 138 if (this.originalMessage == null) { 139 return super.toString(); 140 } 141 return super.toString() + " for original " + this.originalMessage; 142 } 143 144}