1 /*
2  * Copyright 2002-2017 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 module hunt.stomp.support.ErrorMessage;
18 
19 import hunt.stomp.support.GenericMessage;
20 
21 import hunt.stomp.Message;
22 import hunt.stomp.MessageHeaders;
23 
24 import hunt.collection.Map;
25 
26 
27 /**
28  * A {@link GenericMessage} with a {@link Throwable} payload.
29  *
30  * <p>The payload is typically a {@link hunt.stomp.MessagingException}
31  * with the message at the point of failure in its {@code failedMessage} property.
32  * An optional {@code originalMessage} may be provided, which represents the message
33  * that existed at the point in the stack where the error message is created.
34  *
35  * <p>Consider some code that starts with a message, invokes some process that performs
36  * transformation on that message and then fails for some reason, throwing the exception.
37  * The exception is caught and an error message produced that contains both the original
38  * message, and the transformed message that failed.
39  *
40  * @author Mark Fisher
41  * @author Oleg Zhurakousky
42  * @author Gary Russell
43  * @since 4.0
44  * @see MessageBuilder
45  */
46 class ErrorMessage : GenericMessage!(Throwable) {
47 
48 	// 
49 	private MessageBase originalMessage;
50 
51 
52 	/**
53 	 * Create a new message with the given payload.
54 	 * @param payload the message payload (never {@code null})
55 	 */
56 	this(Throwable payload) {
57 		super(payload);
58 		this.originalMessage = null;
59 	}
60 
61 	/**
62 	 * Create a new message with the given payload and headers.
63 	 * The content of the given header map is copied.
64 	 * @param payload the message payload (never {@code null})
65 	 * @param headers message headers to use for initialization
66 	 */
67 	this(Throwable payload, Map!(string, Object) headers) {
68 		super(payload, headers);
69 		this.originalMessage = null;
70 	}
71 
72 	/**
73 	 * A constructor with the {@link MessageHeaders} instance to use.
74 	 * <p><strong>Note:</strong> the given {@code MessageHeaders} instance
75 	 * is used directly in the new message, i.e. it is not copied.
76 	 * @param payload the message payload (never {@code null})
77 	 * @param headers message headers
78 	 */
79 	this(Throwable payload, MessageHeaders headers) {
80 		super(payload, headers);
81 		this.originalMessage = null;
82 	}
83 
84 	/**
85 	 * Create a new message with the given payload and original message.
86 	 * @param payload the message payload (never {@code null})
87 	 * @param originalMessage the original message (if present) at the point
88 	 * in the stack where the ErrorMessage was created
89 	 * @since 5.0
90 	 */
91 	this(Throwable payload, MessageBase originalMessage) {
92 		super(payload);
93 		this.originalMessage = originalMessage;
94 	}
95 
96 	/**
97 	 * Create a new message with the given payload, headers and original message.
98 	 * The content of the given header map is copied.
99 	 * @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 	this(Throwable payload, Map!(string, Object) headers, MessageBase 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 	this(Throwable payload, MessageHeaders headers, MessageBase 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 	
132 	MessageBase getOriginalMessage() {
133 		return this.originalMessage;
134 	}
135 
136 	override
137 	string toString() {
138 		if (this.originalMessage is null) {
139 			return super.toString();
140 		}
141 		return super.toString() ~ " for original " ~ (cast(Object)this.originalMessage).toString();
142 	}
143 
144 }